Ctrl-left and Ctrl-right for Mac OS X Terminal and Emacs
In Emacs, holding Control and pressing the left or right arrow keys moves the cursor backward or forward by one word. It's a very useful way to speed up editing pretty much anything.
If you're using Emacs inside a Mac OS X Terminal window, this might not work for you. By default, Terminal is configured to send ANSI terminal escape sequences in place of C-left and C-right, namely "M-[ 5 d" and "M-[ 5 c", respectively. And by default, Emacs does not recognize these key sequences.
One technique to fix this is to reconfigure Terminal to send different character sequences when you press these keys. For example, by default Emacs also recognizes "ESC b" and "ESC f" as backward-word and forward-word. To reconfigure Terminal to send "M-b" when you press Control-left and "M-f" when you press Control-right:
- From the Terminal menu, select Window Settings..., then select "Keyboard" from the dropdown menu.
- Click on "control cursor left" from the menu, then click the Edit button.
- Click the little "delete" button until the text box is empty. Now press ESC, then "b"; the box should now contain "\033b". Click OK.
- Do the same for "control cursor right," using ESC then "f" as the character sequence.
From now on, pressing C-left and C-right in Terminal will actually send M-b and M-f, which are bound by default in Emacs to mean backward-word and forward-word. Click "Use Settings as Defaults" to make these changes permanent.
Configuring these keys in Terminal has the added advantage that these keys do the same thing in other applications that support M-b and M-f, including ZSH. But you might not want to reconfigure Terminal's behavior; maybe other apps you use work only with Terminal's default behavior. In that case, you probably just want to reconfigure Emacs.
To configure Emacs to recognize the default Terminal behavior "M-[ 5 d" and "M-[ 5 c" as backward-word and forward-word, add the following to your .emacs file:
(global-set-key (kbd "M-[ 5 d") 'backward-word) (global-set-key (kbd "M-[ 5 c") 'forward-word)
Why can't Terminal just send "C-left" and "C-right" to the application on the other end of the line? Actually, it's trying: The default behavior ("M-[ 5 d" and "M-[ 5 c") represents a standard way—one of many—of representing those actions in a terminal protocol. Unfortunately, these sequences don't necessarily get converted back to "C-left" and "C-right" on the other end. The actual behavior depends on the terminal emulator (Terminal), how the emulator declares its "terminal capabilities," or "termcap," (such as "vt100," "xterm", "xterm-color", "ansi"), and how well the thing on the other end supports the specified termcap.
From that, I assume a better solution is to get Terminal, its termcap declaration and the termcap support of the thing Terminal is communicating with to agree on a standard and use it. If anyone has better advice for this than the per-key work-around above, I'd love to hear it.
What are the escape sequence for up-arrow and down-arrow? This information is somehow hard to find.