Linux readline/emacs Cheatsheet

The bash shell has two editing modes: vi and emacs. Although I'm a dedicated vim user, I find myself sometimes inclined to use emacs mode on the command line. Also, the GNU Readline library is used by many applications, so knowing some emacs keystrokes is useful.

Put one or the other in your .bashrc:

set -o vi
set -o emacs

man bash is informative.


C-a    Start of line
C-e    End of line
M-b    Back word
M-f    Forward word
C-f    Forward character
C-b    Backward character
C-xx   Last jump


C-X-e  Open command in $EDITOR (:cq in vim cancels command)
C-_    Undo
C-k    Delete to end of line
C-u    Delete to start of line
M-d    Delete word forwards
C-w    Delete word backwards to whitespace
M-DEL  Delete word backwards to word boundary
C-d    Delete character forwards
C-h    Delete character backwards
C-t    Transpose with next character
M-t    Transpose with next word
M-u    Uppercase word
M-l    Lowercase word
M-c    Capitalize word

M-2, M-3, M-4 etc. sets the number of operations, so M-3 C-w would delete the previous three words.

Shift-Insert pastes from the X clipboard (in most terminals — this isn't are Readline feature).


C-p       Previous in history
C-n       Next in history
M-.      Cycle through last arguments of previous commands
C-r       Start incremental search, start typing remaining command
C-j       Put search result on command line for editing
C-M-y     Insert first argument of previous command
M-n C-M-y Insert nth argument of previous command

We still can use bash history expansion, of course. !! is the last command (synonymous with !-1). !-2 is the command before the last command. !!:^ and !!:$ are respectively the first and last argument in the previous command (and !!:0 !!:1 !!:2 !!:3 index last command).

11:50 falstaff awesome $ echo foo bar bat baz
foo bar bat baz
11:55 falstaff awesome $ echo !!:2-$
bar bat baz

And of course by history number.

--- bava --- ~ $ history 3
  525  echo foo bar bat
  526  top
  527  history 3
--- bava --- ~ $ echo !525:2

Show all keybindings

bind -P | grep 'can be'
stty -a | grep ' = ..;' 

(Thanks to zwischenzugs.)