Sunday 3 December 2017

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


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??



Answer



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

No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...