OS X Terminal equivalent of Ctrl+Left / Ctrl+Right
Solution 1:
You can use the ⌥ key, but you have to select the option to use it as the "meta" key:
Once you do that, ⌥+F and ⌥+B will work as you expect.
Solution 2:
This was driving me crazy too, however I didn't want a Terminal-specific fix as I don't use Terminal, and being able to SSH into my OSX box had to use the correct keybindings too.
Also, personally, I didn't care about the ⌥ key as discussed earlier; I wanted to maintain the ctrl+←/→ combination that I'm used to on Linux.
To solve this problem, I added the following to my ~/.profile
:
bind '"\e[5C": forward-word'
bind '"\e[5D": backward-word'
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'
Note the first two apply to bash in Terminal; the last two apply to bash in iTerm2 and incoming ssh connections. Don't ask me why the keyboard emulation is different ;)
For the record, I was able to find what the keycodes actually were thanks to a hint from this stackoverflow answer: You can run cat > /dev/null
to monitor the exact keycodes sent during a key combination.
(Note that for me, when I run cat > /dev/null
and press ctrl+← it produces ^[[1;5D
. Your exact keycode may differ, but the first ^[
is represented as \e
, as shown in my example code.)
BTW, if you want to see all active bash keybindings, run bind -p
.
Solution 3:
As of Mac OS X Lion 10.7, Terminal maps ⌥-←/→ to esc+B/F by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.
Solution 4:
if you know how to use vi you can also turn on vi line editing mode using set -o vi
in your .bash_profile
or at any time on the command line. Then you can switch between vi command and insert modes. So you could hit escape, then use the vi commands for navigating the line:
0 = move to beginning of line
$ = move to end of line
w = move forward one word
b = move backward one word
Once you get to the correct position, you can use the other vi commands to enter insert or append modes, or remove characters one by one, etc.