In Windows and Linux I could always hit ctrl+← or ctrl+→ to skip a word to the left or right in the terminal. In text editors it seems that ⌥+←/→ works, but no dice in the terminal. I've searched Google and found nothing except a Wikipedia page that claims ⌥+B/F does the job, but all it does is print ∫ and ƒ characters to my terminal (or ı and Ï if I hold ⇧). Any ideas?
7 Answers
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.
-
awesome... this has been driving me crazy for a long time. thanks. Commented Sep 20, 2010 at 3:41
-
2Also check apple.stackexchange.com/q/1391/115 for possible “drawbacks” of that option. Commented Dec 4, 2010 at 7:01
-
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
.
-
I like to use
xxd
to examine keyboard input. You can see the character displayed as you type it, thenxxd
emits the value in hex when you type return. Commented Oct 24, 2011 at 19:48 -
Perfect! Just what I was looking for :) Never had the luck of making the "App-specific" settings above, but this just works!– biskoCommented Nov 9, 2011 at 13:22
-
Make sure control+left/right aren't mapped to "switch spaces" in your sysprefs > keyboard > shortcuts > exposé. I had no other spaces open, so it just looked like my keyboard input wasn't working.– sudoCommented Sep 20, 2018 at 17:57
-
+1 for telling us how to generate the key stoke and debug the problem!!– daisyCommented Jul 6, 2023 at 8:17
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.
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.
-
I never knew you could use 0 for start of line, I always used ^ Commented Jan 3, 2011 at 16:47
-
1@vxjasonxv: ya, ^ moves to the first non-whitespace char, 0 moves to the first column (which you probably already noticed but I figured I'd throw it out there for others :P) Commented Jan 3, 2011 at 20:36
-
1OH WOW, THAT IS EVEN BETTER, I hadn't noticed that yet! Oh man, that functionality (first non-whitespace) of ^ always pissed me off. Thank you. Commented Jan 4, 2011 at 0:17
-
Of course,
ESC ^
is great when you do need it, it's only annoying when you wanted to do something else entirely :)– ocodoCommented Oct 25, 2011 at 1:39
Open terminal, go to preferences, keyboard. On the list you'll find “ctrl cursor right” and “ctrl cursor left”.
Change their assigned actions to “\033f” and “\033b” respectively, and you'll be able to use ctrl+→ and ctrl+← to move, without losing your alt key.
It seems that esc+B and esc+F are mapped by default to go to the beginning and end of words. Although, for my system you can't hold down esc but have to re-press it for each word.
-
1Esc is never a modifier, it's just going into "Escape" mode when you press it, consider it a modal way of doing things, unlike shift/ctrl/meta etc. (in the Readline scheme of things anyway.)– ocodoCommented Jan 29, 2011 at 9:40
Latest version of OS maps this to Spaces. Even if you have Spaces turned off, the hotkeys can still get short-circuited in this hidden location: