Ctrl+Left and Ctrl+Right don't move by word over SSH: key codes and ~/.inputrc
Ctrl+Left and Ctrl+Right print ;5D or 5C, or do nothing, in bash over SSH. What the keys send, why readline ignores them, and the ~/.inputrc fix.
In bash on your own computer, Ctrl+Left and Ctrl+Right jump a word at a time. Over SSH on another host they print ;5D and ;5C, or 5D, or do nothing. The keys are sent fine; the shell on the host does not know what they mean.
What the keys send
An xterm-compatible terminal sends a cursor key with a modifier number: 5 for Ctrl, 3 for Alt, 2 for Shift. Ctrl+Left is ESC [ 1 ; 5 D and Ctrl+Right is ESC [ 1 ; 5 C. The terminfo entry for xterm-256color lists them as kLFT5=\E[1;5D and kRIT5=\E[1;5C.
To see what your terminal really sends, run cat -v (or press Ctrl+V in bash) and press the key:
$ cat -v
^[[1;5D^[[1;5C
^[ is ESC. Press Ctrl+C to leave cat.
Why bash ignores them
bash reads keys through readline. Readline's own word commands are forward-word (Alt+F) and backward-word (Alt+B). Whether the Ctrl+arrow sequences are bound to them depends on the readline startup file:
- Readline reads the file named in
INPUTRC, or else~/.inputrc. - Only if
~/.inputrcdoes not exist does it read/etc/inputrc.
On some systems /etc/inputrc binds the Ctrl+arrow keys. So two likely causes are a host whose /etc/inputrc does not bind them, and a personal ~/.inputrc that exists, so /etc/inputrc is never read. You can list what is bound with:
bind -p | grep -E 'forward-word|backward-word'
The fix
Add these lines to ~/.inputrc on the host. The first line keeps the system file's settings when you have your own file:
$include /etc/inputrc
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[1;3C": forward-word
"\e[1;3D": backward-word
The last two make Alt+Right and Alt+Left work too. Log in again, or press Ctrl+X Ctrl+R to reread the file.
If cat -v shows a different sequence, such as ^[[5D or ^[OD, bind the sequence you see. A different sequence can come from a different TERM or terminal setting; see Keys and key codes.
Programs that do not use readline, such as vim and emacs, have their own key settings.
Related
- Keys and key codes
- TERM and terminfo
- Shell and tmux basics
- App guide: What the keyboard sends in a VT session