20

How do I configure Ctrl-Left and Ctrl-Right as previous/next word shortcuts for bash (currently alt-b and alt-f)?

ripper234
  • 31,763

2 Answers2

27

The correct answer depends on which terminal you are using.

For Gnome Terminal or recent versions of xterm, put this in ~/.inputrc:

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

For PuTTY, put this in your ~/.inputrc:

"\eOC": forward-word
"\eOD": backward-word

For rxvt, put this in your ~/.inputrc:

"\eOc": forward-word
"\eOd": backward-word

You can probably get away with putting all of those together in ~/.inputrc.

In all cases, you also need to put this in your ~/.bashrc (or ~/.zshrc):

export INPUTRC=~/.inputrc

If that doesn't work, or you have a different terminal, go to your terminal and type Ctrl+V Ctrl+->. Then use that instead of "\e[1;5C" or "\eOC" above. Repeat for Ctrl+<-.

Note that you need to write the keyboard escape sequences using the inputrc syntax, e.g.

  • \C means control
  • \e means escape (which appears as ^[ when typing it using Ctrl+V above)
Mikel
  • 57,299
  • 15
  • 134
  • 153
  • Ctrl+V, Ctrl--> prints "^[[1;5C", and I'm using gnome-terminal. – ripper234 Apr 07 '11 at 09:06
  • 2
    So try "\e[1;5C": forward-word. And do the Ctrl+V thing for Ctrl+Left Arrow too. – Mikel Apr 07 '11 at 09:21
  • Thanks, works like a charm. Slowly but surely, I'll learn how to swim in linux. Why did they have to make it so non-intuitive in the first place? (You have to admit ctrl-left, ctrl-right are better choices) – ripper234 Apr 07 '11 at 10:25
  • L33t! I've always hated the Emacs-like shortcuts in bash, but if I can make them conform to more Vim-like... Yes for my system I used this: "\e[1;5C": forward-word and "\e[1;5D": backward-word – penguin359 Apr 07 '11 at 23:23
  • @ripper234 All the standard short cuts are based on Emacs, and Emacs picked them because they were cross-platform. Arrow keys have not always been available, and when they were, they didn't always agree on what sequence to send, especially when modified by Ctrl. Alt on the other hand, just puts an Esc character in from of the key. While Alt hasn't always been available, Esc has been present since the VT-52 in the 1960s. – penguin359 Apr 07 '11 at 23:27
  • 1
    If you think Linux is non-intuitive to begin with, try a default install of FreeBSD or even Solaris. Having bash as your starting shell is a dream. Also, since keyboards didn't always have Arrow keys, Emacs, and hence bash support shortcuts for that too. Try using Ctrl-b and Ctrl-f for a while. – penguin359 Apr 07 '11 at 23:31
  • 2
    The beauty of UNIX/Linux systems is it's easy to move around settings. Once you get your favorite programs configured the way you like them, just copy it around. I'm actually using version control for my home directory files like .inputrc, .bashrc, .vimrc, and so on. I use mercurial to clone and keep all my home directory settings up to date between computers. I'm also planning at some point to publish my repo so others can share it all my hard work. – penguin359 Apr 07 '11 at 23:37
  • \e[1;5C actually puts the cursor at the first space forward. Is there a code which puts it at the first character of the next word forward? – paradroid Jun 13 '11 at 23:40
  • @jason404: No, not that I can find. Bash is designed to use Emacs-style movement, which works that way. zsh works the way you prefer. – Mikel Jun 16 '11 at 03:25
  • @Mikel: Thanks. Do you know where I can find a list of these key codes? And are they different for, say, VT100 and Xterm? – paradroid Jun 17 '11 at 18:17
  • @jason404: Compare output of infocmp and bind (bash)/bindkey -l (zsh). – Mikel Jun 17 '11 at 21:38
  • Hi, What code should I put in inputrc if I want SHIFT+arrow_keys to navigate? Also how to apply these changes, restart the computer? – user13107 Sep 26 '13 at 02:25
3

If you're using ZSH, you need to take a different method, because, quoting the relevant parts from ArchLinux's ZSH guide:

Zsh does not use readline #like bash#, instead it uses its own and more powerful zle.

It does not read /etc/inputrc or ~/.inputrc.

After reading that guide, I figured I need a modren source of ZSH to get the general idea of how to do this from, and so I found this oh-my-zsh file which describes the behaviour I wanted, I copied the relevant parts I wanted into my ~/.zshrc:

bindkey -e #Use emacs mode, it's more sane for beginners
bindkey '^[[1;5C' forward-word # [Ctrl-RightArrow] - move forward one word
bindkey '^[[1;5D' backward-word # [Ctrl-LeftArrow] - move backward one word
# Also, If you want to copy paste this into your zshrc,
# I'm pretty sure you would have to add this at the top : 
# setopt interactive_comments