Enable alt/ctrl + left/right on CentOS command line

10

4

My hoster recently updated to a new CentOS and I've been 'personalizing' it and now I'm missing something.

On my home Ubuntu Server I can forward-word and backward-word with ALT + RIGHT / LEFT. I didn't do anything for that. I can also ALT + BACKSPACE to delete a word.

On my new CentOS I can do the ALT + BACKSPACE, but I can't get the ALT + RIGHT / LEFT working! ALT + B / F work, but that's just not acceptable, because it only works for the left ALT and my fingers aren't that lean. (right ALT + B / F just prints "b" or "f")

I've tried copying a part of /etc/inputrc to CentOS, but that doesn't do anything.

ALT or CTRL, I don't care, but I really need a shortcut. The last CentOS didn't have it, but since Ubuntu I've been missing it on CentOS.

My CentOS /etc/inputrc:

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

My Ubuntu /etc/inputrc:

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

I don't even know what they all mean!? What is [1;5C for key??

Rudie

Posted 2013-01-04T23:29:00.957

Reputation: 519

I just use the first 2 lines (\e[1;5C and \e[1;5D) you quote from your Ubuntu inputrc in my ~/.inputrc and it works fine for debian, ubuntu and arch. – terdon – 2013-01-05T01:02:17.107

For ALT or CTRL? Since those are in there, they should work, wouldn't you think? Two of them must be B and F, I would think. Linux is weird. – Rudie – 2013-01-05T01:39:04.527

They work for CTRL. My guess is that order is important, one may overwrite the other. Also, local settings files (e.g. ~/.inputrc) take precedence over system-wide ones, so if there is a weird conflict somewhere a local file might fix it. – terdon – 2013-01-05T02:05:13.030

Answers

11

Thanks to your question I finally did some reading and increased my understanding, cheers!

So, a very good source of information is man readline. The keybindings specified in the various inputrc files control the way that the BASH readline library works. According to the readline manpage you can use either symbolic key names or escape sequences:

   Key Bindings
       The syntax for controlling key bindings in the inputrc file is
       simple.  All that is required is the name of  the  command  or
       the  text  of a macro and a key sequence to which it should be
       bound. The name may be specified in one of two ways: as a sym‐
       bolic  key  name, possibly with Meta- or Control- prefixes, or
       as a key sequence.  The name and key sequence are separated by
       a  colon.  There can be no whitespace between the name and the
       colon.

       When using the form keyname:function-name or macro, keyname is
       the name of a key spelled out in English.  For example:

              Control-u: universal-argument
              Meta-Rubout: backward-kill-word
              Control-o: "> output"

The man page also states that the default configuration file is ~/.inputrc so I recommend placing your bindings there.

If you want to use normal letter keys (for example Control-g), Control-g: forward-word works fine. The arrow keys are harder. I tried, and failed, to find the key name for the arrow keys. None of the ones I tried (left-arrow, left, :left) worked so it seems like we are stuck with the escape sequences.

Unfortunately, the exact escape sequence differs between terminal emulators (that is why your Ubuntu inputrc had multiple lines). To find out which escape sequence your favorite terminal uses, run read and then type the key sequence you are interested in. In terminator, xterm and gnome-terminal, Control-Left give:

$ read
^[[1;5D

in aterm:

$ read
^[Od    <-- that is a capital O not a zero (0).

By experimenting a bit, I figured out that ^[[D is Left and ^[[1;5D is Control-Left. The first ^[ is the Esc key, used here, I suppose, to denote an escape sequence.

In any case, to bind Control-Left to forward-word in a way that works for all, I added these lines to my ~/inputrc:

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

For reasons I have not fully understood, Control is represented by \e which should be Esc.

My final ~/.inputrc file that works for all the terminals listed above is:

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

terdon

Posted 2013-01-04T23:29:00.957

Reputation: 45 216

1YES YES YES! You are my absolute hero! It's perfect now! My tiny air keyboard now uses right ctrl + arrows and my main/desktop keyboard now uses left alt + arrow keys. Perfect!! Somehow my air keyboard's right alt doesn't trigger anything in read... Probably the keyboard's fault (some special function!?). Awesome! You the man! – Rudie – 2013-01-05T15:53:54.820

My settings are a bit different btw: ctrl left / right: \eOD / \eOC and alt left / right: \e\e[D / \e\e[C. Thank god it's a readable, sensible syntax like this, right!? Linux rocks! – Rudie – 2013-01-05T15:58:13.147

Yeah, got to admit the syntax could be better, probably a historical relic. Glad it works! – terdon – 2013-01-05T17:31:13.300

1

How to do it in Iterm2 so that it works when you connect to any host.

Note: I only write this because, another question (https://superuser.com/questions/1042937/getting-ctrlarrows-to-move-word-at-a-time-on-newest-iterm2?rq=1) has linked to this question as a duplicate, but this question does not answer how to do this in Iterm2, so it isn’t really a duplicate.

If you are on OS X and using Iterm2 you can map Alt/Ctrl+→ (right arrow) and Alt/Ctrl+ to the standard readline commands Meta+F and Meta+B. In the example below it is assumed that the escape key is meta. To do this, open Iterm2.

  1. Click: Menu -> Preferences...
  2. Click “Profiles” tab.
  3. Click your profile or the default one.
  4. Click the “Keys” sub tab.
  5. Click the “+” plus sign next to “Load Preset…”
  6. Type your keyboard shortcut in the first field (e.g., Ctrl+)
  7. Choose either “Send Text”, “Send Hex” (more on sending Hex in “How to bind a key sequence to the control key” below) or “Send Escape Sequence”. In this case we are using "Send Escape Sequence”.
  8. In the last field choose characters that readline can interpret.
  9. ^[ is the readline sequence for escape. This entry maps Opt+ to Esc+F (may be Alt+F in other terminals)

key mapping window example

How to map shortcuts to non-escape sequences (Ctrl, Alt, Cmd, etc.)

  1. Download “Key Codes” from the app store. Install it and open it.
  2. While in an open Key Codes window, hit the Ctrl+[some key], in this case I’ll use Ctrl+W for deleting a previous word in readline.
  3. Note the “Unicode” field.
  4. Now map your key sequence using in iterm, preferences->Keys->+ (the sequence provided above).
  5. Enter “Keyboard Shortcut” field something like in this situation Opt+Backspace.
  6. Change “Action” to “Send Hex Code”.
  7. Enter in last field the previously noted hex; in this case, 23 / 0x17.
  8. Click “OK" and the shortcut should now be operational in your terminal window and work on any system where readline is available.

N D

Posted 2013-01-04T23:29:00.957

Reputation: 179

0

Thanks for the question. Had this problem with PuTTY on CentOS 7 and solved by adding this inside the $if mode=emacs conditional:

# for PuTTY
"\e\e[C": forward-word
"\e\e[D": backward-word

Activated the setting with Ctrl + X then Ctrl + R

Walf

Posted 2013-01-04T23:29:00.957

Reputation: 254