23

In bash, you can move to the beginning of the line with CTRL+A, and the end with CTRL+E. How can I move forward and backward by word?

1

3 Answers 3

24

With emacs bindings:

Meta-B moves back a word and Meta-F moves forward a word.

Ctrl-B moved back a character and Ctrl-F moves forward a character.

So B vs F is backwards vs forward and Meta vs Ctrl is word vs character.

The exact mapping of Meta may vary between keyboards. Try holding down Alt while pressing the other key; if that doesn't work, press and release Esc and then press the other key.

8

use alt+b for backward and alt+f for forward movement by a word.

5

Put in ~/.inputrc:

# Ctrl+Left/Right to move by whole words.
"\e[1;5C": forward-word
"\e[1;5D": backward-word
# Same with Shift pressed.
"\e[1;6C": forward-word
"\e[1;6D": backward-word

showkey -a in the terminal will tell you which ANSI codes are emitted by terminal on key press, for Ctrl+Right I got:

bash# showkey -a

Press any keys - Ctrl-D will terminate this program

^[[1;5C          27 0033 0x1b
         91 0133 0x5b
         49 0061 0x31
         59 0073 0x3b
         53 0065 0x35
         67 0103 0x43

^[ is ESC which is \e in ~/.inputrc. You can try it on the fly with Bash built-in bind:

bind '"\e[1;5C": forward-word'

or dump current bindings with:

bind -p

You must log in to answer this question.