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

  • Um, doesn't this question ring a bell? – Gilles 'SO- stop being evil' Apr 7 '11 at 19:18
  • @Gilles - I thought that I asked something similar before, yes, sorry. Closed the original as a dup (because it belongs more here than at Super User). – ripper234 Apr 8 '11 at 8:32

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)
  • Ctrl+V, Ctrl--> prints "^[[1;5C", and I'm using gnome-terminal. – ripper234 Apr 7 '11 at 9:06
  • 2
    So try "\e[1;5C": forward-word. And do the Ctrl+V thing for Ctrl+Left Arrow too. – Mikel Apr 7 '11 at 9: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 7 '11 at 10:25
  • 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 7 '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 7 '11 at 23:37

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
  • i was trying ctrl+rightarrow, but it doesn't travel a word on Mac – TomSawyer Nov 24 '19 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.