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
M-_      Cycle all arguments of previous commands (this never seems to work for me)
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).

$ echo foo bar bat baz
foo bar bat baz
$ echo !!:2-$
bar bat baz

And of course by history number. Get the third argument from history command 5066:

$ echo $5066:3
$ history 3
  525  echo foo bar bat
  526  top
  527  history 3
$ echo !525:2

Show all keybindings

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

(Thanks to zwischenzugs.)

GNU Readline (from Wikipedia)

Taken from the Wikipedia GNU Readline page:

On some systems, Esc must be used instead of Alt, because the Alt shortcut conflicts with another shortcut.
For example, pressing Alt+f in Xfce's terminal emulator window does not move the cursor forward one word, but activates "File" in the menu of the terminal window, unless that is disabled in the emulator's settings.

Tab ↹ : Autocompletes from the cursor position.
Ctrl+a : Moves the cursor to the line start (equivalent to the key Home).
Ctrl+b : Moves the cursor back one character (equivalent to the key ←).
Ctrl+c : Sends the signal SIGINT to the current task, which aborts and closes it.
Ctrl+d : Sends an EOF marker, which (unless disabled by an option) closes the current shell
	(equivalent to the command exit). (Only if there is no text on the current line)
	If there is text on the current line, deletes the current character (then equivalent to the key Delete).
Ctrl+e : (end) moves the cursor to the line end (equivalent to the key End).
Ctrl+f : Moves the cursor forward one character (equivalent to the key →).
Ctrl+g : Abort the research and restore the original line.
Ctrl+h : Deletes the previous character (same as backspace).
Ctrl+i : Equivalent to the tab key.
Ctrl+j : Equivalent to the enter key.
Ctrl+k : Clears the line content after the cursor and copies it into the clipboard.
Ctrl+l : Clears the screen content (equivalent to the command clear).
Ctrl+n : (next) recalls the next command (equivalent to the key ↓).
Ctrl+o : Executes the found command from history, and fetch the next line relative to the current line from the history for editing.
Ctrl+p : (previous) recalls the prior command (equivalent to the key ↑).
Ctrl+r : (reverse search) recalls the last command including the specified character(s).
	A second Ctrl+r recalls the next anterior command that corresponds to the search
Ctrl+s : Go back to the next more recent command of the research
	(beware to not execute it from a terminal because this command also launches its XOFF).
	If you changed that XOFF setting, use Ctrl+q to return.
Ctrl+t : Transpose the previous two characters.
Ctrl+u : Clears the line content before the cursor and copies it into the clipboard.
Ctrl+v : If the next input is also a control sequence, type it literally
	(e. g. * Ctrl+v Ctrl+h types "^H", a literal backspace.)
Ctrl+w : Clears the word before the cursor and copies it into the clipboard.
Ctrl+x Ctrl+e : Edits the current line in the $EDITOR program, or vi if undefined.
Ctrl+x Ctrl+r : Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there.
Ctrl+x Ctrl+u : Incremental undo, separately remembered for each line.
Ctrl+x Ctrl+v : Display version information about the current instance of Bash.
Ctrl+x Ctrl+x : Alternates the cursor with its old position. (C-x, because x has a crossing shape).
Ctrl+y : (yank) adds the clipboard content from the cursor position.
Ctrl+z : Sends the signal SIGTSTP to the current task, which suspends it.
	To execute it in background one can enter bg.
	To bring it back from background or suspension fg ['process name or job id'] (foreground) can be issued.
Ctrl+_ : Incremental undo, separately remembered for each line.
Alt+b : (backward) moves the cursor backward one word.
Alt+c : Capitalizes the character under the cursor and moves to the end of the word.
Alt+d : Cuts the word after the cursor.
Alt+f : (forward) moves the cursor forward one word.
Alt+l : Lowers the case of every character from the cursor's position to the end of the current word.
Alt+r : Cancels the changes and puts back the line as it was in the history.
Alt+u : Capitalizes every character from the cursor's position to the end of the current word.
Alt+. : Insert the last argument to the previous command (the last word of the previous history entry).