21

I use Ctrl- and Ctrl- on every Linux system I work on, but these key combinations don't work in bash on my Macbook Pro. It is running Snow Leopard, and I'm using the built in Terminal application.

The key combinations don't seem to be captured by anything before or by Terminal, because if I ssh to one of my Linux servers, I can use Ctrl-/ to jump to the next/previous word on the command line. However, when I try this key combination in bash on my Macbook, I just get "C" for right arrow and "D" for left arrow.

Any ideas on how I can get these keys to work in darwin bash?

2
  • 2
    If you are using Spaces, Ctrl + arrow by default switches spaces.
    – mark4o
    Commented Oct 31, 2010 at 21:19
  • 1
    Similar Commented Oct 31, 2010 at 21:36

6 Answers 6

17

OS X uses emacs key binding my default. This is true is virtually every application on OS X, it's rather nice. It means things like C-a and C-e are beginning/end of line. You also get the nifty backward-word-kill with M-backspace, oh, and kill-line with C-k.

This should mean that in your terminal forward/backward-word are bound to M-f and M-b, respectively (M = Meta = alt/option), however that is not the case. On OS X forward/backword-word are bound to M-→ and M-← by default.

You can alter this behavior by changing how the GNU Readline Library is configured for your account. This takes place in your ~/.inputrc file. You can get a big list of bindable commands with man readline as well as in the online documentation like this here..

So to answer your question, you want to remap what Readline does when it sees C-→ and C-← to do what it does on your linux server.

The syntax for a ~/.inputrc file is pretty simple for what you want to do: key-sequence: action.

This should be what you need to get the desired behavior:

"\e[5C": forward-word
"\e[5D": backward-word

Here's another page with additional useful bindings.

(You could probably get away with copying /etc/inputrc from your linux box to your OS X ~/.inputrc)

4
  • A note: If you are using tmux with xterm-keys a lot, you will also need \e[1;5C and \e[1;5D to also trigger forward-word and backward-word.
    – Arne
    Commented Jan 26, 2014 at 10:45
  • 6
    Another note: If still not working, it may be because of a shortcut conflict with Mission Control/Spaces. System Preferences -> Keyboard -> Shortcuts -> Mission Control, Move left/right a space
    – bhh1988
    Commented Jul 7, 2014 at 5:25
  • I don't understand "OS X uses emacs key binding by default." "Ctrl-arrow" is also an emacs key binding.
    – Greg
    Commented Mar 8, 2019 at 2:45
  • @bhh1988 Lifesaver ! Thanks a lot, I was going crazy Commented Nov 20, 2020 at 16:06
4

UPDATE Jan 2020

On the newest versions of OSX the default shell is zsh. Therefore the magic incantation to get ctrl-arrowleft and ctrl-arrowright to work as expected is the following:

bindkey '^[[1;5D' backward-word
bindkey '^[[1;5C' forward-word

(put it into your .zshrc)

2
  • 2
    I feel like this is probably one of the most common scenarios for people arriving at this from a google search. imo this should be the accepted answer - thanks for this!
    – J.Wolfe
    Commented Sep 15, 2021 at 0:57
  • Thank you sooo much <3
    – Bostrot
    Commented Feb 18, 2023 at 10:26
3

put in ~/.inputrc following lines:

"\e[5C": forward-word
"\e[5D": backward-word
2

As @bhh1988 mentioned, the Mission Control configuration prevents the Ctrl-Keys working with the arrow keys in bash. I didn't want to interfere with those shortcuts, so I use the Option (i.e. Alt) key instead. Currently on Mojave using a German keyboard (don't know if that's relevant) I put the following in .inputrc to use Option-left-arrow and Option-right-arrow to move between words.

"\e\e[C": forward-word
"\e\e[D": backward-word
1
  • Disabling the Ctrl Left and Ctrl Right shortcuts used by Mission Control in Keyboard settings solved the issue for me.
    – Alex
    Commented Oct 28, 2021 at 11:50
1

These days it seems that the escape sequences that Ctrl-Arrows output in the terminal have changed. Here is what works for me as of today (end of 2018):

"\e[1;5C": forward-word
"\e[1;5D": backward-word

Note: you can always figure out the actual escape sequences by typing cat -v in the Terminal, and pressing Ctrl-Left Arrow or Ctrl-Right Arrow. Here is a sample output for my own Ctrl-Left Arrow:

^[[1;5C
^[[1;5C

To translate into .inputrc lingo, replace the escape sign ^[ with \e and stick the result inside the double quotes.

1
  • This is what worked for me on Linux. The \e[5C / \e[5D codes didn’t work for me. Commented Dec 5, 2022 at 14:21
1

In more recent macOS versions (at least Ventura) and zsh, which is now the default shell, just adding keyboard bindings to the profile settings in Terminal seems to be enough:

  • C-→ \033[1;5C
  • C-← \033[1;5D

Changing the config files was not necessary.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .