96

I am use to using the CTRL key to move faster when using the left and right arrow keys (goes to end of a word, instead of one char at a time).

Can I do that in bash somehow?

I could probably code it, but I was wondering if there is something easier / already done.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Bryan Ruiz
  • 2,555
  • 5
  • 29
  • 35

4 Answers4

118

With the default readline key bindings, ALT+B goes back one word, ALT+F goes forward one word.

The default Ubuntu setup additionally provides CTRL+arrows like you're used to. These are in /etc/inputrc and specified as follows:

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\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

Not sure why we need three of them...

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Its interesting that the bindings you specified dont match the bindings I figured out and mentioned in my answer. I wonder what that discrepancy is. Also, I am curious how you can determine what a character sequence is when looking at /etc/inputrc. (for example. how do I know what 1;5C means above?) – Bryan Ruiz Feb 17 '11 at 14:11
  • 20
    @bryan_ruiz: Different keyboards (terminal emulators, etc.) output different sequences. To see the sequence press Ctrl-v then the key. For me in PuTTY, Ctrl-v Ctrl-RightArrow gives me `^[OC` which is the same as yours. In `xterm` I get `^[[1;5` which is the same as Thomas'. – Dennis Williamson Feb 17 '11 at 18:52
  • May I know why you gave 3 pairs? I thought one would suffice? – Viet Nov 15 '12 at 01:58
  • 7
    it would be better to do so by editing ```~/.inputrc``` instead – svassr Mar 20 '13 at 22:36
  • 2
    I can't believe how long I lived without this. – Ryan Shillington Dec 17 '13 at 20:19
  • 4
    Editing ~/.inputrc may stop the 'system' one (/etc/inputrc) being loaded. Also note, the 'set -o' settings -- "set -o emacs" should give you emacs style key bindings on the command line, as opposed to e.g. "set -o vi" (after doing this you'll need to press ESC before trying to use w or b). – David Goodwin Apr 30 '14 at 14:01
  • We also have [a similar question for PuTTY in particular](http://superuser.com/a/103097/45163). – palswim Nov 17 '16 at 21:28
  • This config file makes alt+arrow keys work on OSX.. dunno what the escape sequence is for ctrl on mac.. – John Hunt Dec 07 '16 at 08:50
  • 5
    @DavidGoodwin After a bit of searching, adding `$include /etc/inputrc` will also load the system inputrc file. – SirGuy Nov 26 '18 at 14:44
44

As Thomas explained, you can add the bindings to /etc/inputrc.

Another alternative so it loads every time you log in, is putting them in ~/.bashrc like this:

#use ctl keys to move forward and back in words
bind '"\eOC":forward-word'
bind '"\eOD":backward-word'

I learned that you can use cat > /dev/null to look at the characters that your keyboard is sending, e.g., CTRL + right arrow shows:

^[OC

where ^[ is the same as \e so that's where the code comes from in the bind command.

You can also look up bindings like this:

bind -p | grep forward-word

All of this is pretty damn awesome and I'm glad I found out some more power of bash.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Bryan Ruiz
  • 2,555
  • 5
  • 29
  • 35
  • 8
    I *think* this will only affect bash, whereas the `inputrc` solution will also apply to other programs that use readline, e.g. the Python interpreter, most other shells. Note that you can also put the commands in `~/.inputrc` if you don't have root, or don't want them to apply to all users. – Thomas Feb 17 '11 at 14:47
  • For whatever reason this only worked for me when I swapped the order of your lines above. Further note. I'm using Ubuntu 14.04LTS behind putty from Windows 8.1. – Rebecca Dessonville Jan 10 '15 at 19:44
  • 2
    +1 especially for `I learned that you can use cat > /dev/null to look at the characters that your keyboard is sending`. #TIL – Sungam Mar 30 '17 at 01:21
  • 4
    You can also simply type `read` and hit enter to achieve the same effect as `cat > /dev/null`. I'm lazy so I like solutions with less typing. :) – theglossy1 Jun 16 '17 at 14:14
  • ctrl+4 crashes my cat (it sends ^/) – user1040495 Jun 21 '18 at 22:21
9

A .inputrc in your home directory will cause ctrl+left to stop working on Ubuntu (for example).

To get everything working, add the following to ~/.inputrc:

# Include system-wide inputrc, which is ignored by default when
# a user has their own .inputrc file.
$include /etc/inputrc

credit to f.kowal

  • I was affected by this part. I have ~/.inputrc for `set enable-bracketed-paste off`, because of which my ctrl+left/right stopped working. Your solution helped me. +1. – anishsane Feb 07 '23 at 15:53
1

Add $include /etc/inputrc

in ~/.inputrc

Worked for CentOS Linux release 8.2.2004 (Core)

Pablohe
  • 11
  • 1