How to change previous/next word shortcut in bash?

110

Solution 1

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)

Solution 2

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
Share:
110

Related videos on Youtube

Sadegh
Author by

Sadegh

Updated on September 18, 2022

Comments

  • Sadegh
    Sadegh about 1 year

    i'm using this rule for rewriting subdomain

    and i'm corrected host file to

    127.0.0.1 domain.net

    when user typed news.domain.net/default.aspx it must return domain.net/news/default.aspx but browser showed Address not found.

    how do i?

    please help

    thanks all

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 12 years
      Um, doesn't this question ring a bell?
    • ripper234
      ripper234 over 12 years
      @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
    ripper234 over 12 years
    Ctrl+V, Ctrl--> prints "^[[1;5C", and I'm using gnome-terminal.
  • Mikel
    Mikel over 12 years
    So try "\e[1;5C": forward-word. And do the Ctrl+V thing for Ctrl+Left Arrow too.
  • ripper234
    ripper234 over 12 years
    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)
  • penguin359
    penguin359 over 12 years
    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
    penguin359 over 12 years
    @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
    penguin359 over 12 years
    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
    penguin359 over 12 years
    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.
  • paradroid
    paradroid over 12 years
    \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?
  • Mikel
    Mikel over 12 years
    @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.
  • paradroid
    paradroid over 12 years
    @Mikel: Thanks. Do you know where I can find a list of these key codes? And are they different for, say, VT100 and Xterm?
  • Mikel
    Mikel over 12 years
    @jason404: Compare output of infocmp and bind (bash)/bindkey -l (zsh).
  • user13107
    user13107 about 10 years
    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?
  • elkirkmo
    elkirkmo almost 4 years
    i was trying ctrl+rightarrow, but it doesn't travel a word on Mac