4

I'm trying to make the following bindings in my ~/.inputrc:

(Ctrl+Del): kill-word
(Ctrl+Backspace): backward-kill-word
(Ctrl+forward arrow): forward-word
(Ctrl+back arrow): backward-word
(Home): beginning-of-line
(End): end-of-line

What actual key sequence notations should I use for those?

1234ru
  • 141

2 Answers2

4

Something like this:

"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[H": beginning-of-line
"\e[F": end-of-line
"\eOH": beginning-of-line
"\eOF": end-of-line
"\e[1~": beginning-of-line
"\e[4~": end-of-line

Depending on the terminal settings and whether you're using screen, xterm would send those variations for Home and End, while the Control modified-cursor keys send a consistent value. You could change that (with xterm: other terminals generally not).

The control/Del and control/backspace probably won't work, unless by Del you mean the lower-left key on the 6-key editing keypad. For xterm, that sends "\e[3;5~".

With putty, you have few choices for "ctrlarrows" because it uses the control-modifier to switch between the normal and application modes of the cursor keys. That is, you'd toggle between "\e[C" (normal) and "\eOC" (application). Also, it's possible to change the home/end (to what putty's developers called "rxvt") to get the "\e[H" mentioned above.

Further reading:

Thomas Dickey
  • 76,765
  • "\e[1~" as Home and "\e[4~" as End worked for me, while others didn't. Still something, though! – 1234ru May 12 '17 at 20:09
2

Function and cursor keys send an escape sequence that begins with the ESC (escape) character. Press Ctrl+V then the key or keychord. This inserts the ESC character literally, followed by the rest of the characters in the escape sequence.

For example, suppose that you press Ctrl+V Ctrl+Left and it inserts ^[O5D. ^[ is the escape character. Then in .inputrc you would bind it with

"\eO5D": backward-word

The escape sequence for a given key depends on the terminal, so you may end up binding more than one escape sequence to the same function, depending on what terminals you use and how you configure them. The good news is that by and large there are no escape sequences that correspond to different keys on different terminals.

Many terminals do not send distinct escape sequences for all keychords. For example you may find that Ctrl+Left and Ctrl+Shift+Left send the same escape sequence. There may or may not be a way to fix this in the terminal configuration; see this post for an Emacs-oriented discussion. See also How to make a comprehensive set of possibilities for defining GNU-screen "command characters"? and How do keyboard input and text output work? and How to make a comprehensive set of possibilities for defining GNU-screen "command characters"? for some background on the topic.

  • @Giles, thank you for the info. I tried to find out a sequence for Ctrl+Left and got ^[0D. However, "\e0D": backward-word in .inputrc gives no effect (while Alt+b, default for this command, keeps working). – 1234ru May 12 '17 at 20:03
  • @1234ru Look closer. I'm pretty sure it's ^[OD (capital letter O, not 0). ^[OD is one of the common escape sequences for Left (and then ^[OA for Up, etc). Note that this would mean that Ctrl+Left and Left send the same escape sequence, so to bind Ctrl+Left to a different command you'll need to configure your terminal differently or use a different one. – Gilles 'SO- stop being evil' May 12 '17 at 23:48
  • @Giles my bad, you're right (I've noticed that myself but then occasionally printed 0 instead of O in the config). It works! – 1234ru May 17 '17 at 20:27
  • @Giles by the way, Left sends [D, not OD (as does Ctrl+Left) in my terminal. – 1234ru May 17 '17 at 20:28