Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash readline shortcuts

Reference for Bash command-line editing with Readline: movement, deleting, history search, undo, emacs versus vi mode, bind and customizing keys in ~/.inputrc.

Readline is the library Bash uses to read the line you type. It provides keyboard shortcuts for moving the cursor, cutting and pasting text, searching history and completing names. The same library is used by Python's REPL, psql, mysql and many other programs, so learning the shortcuts pays off in many places.

By default Readline uses emacs mode, with key bindings such as Ctrl+A and Ctrl+E. If you prefer vi, set -o vi switches to a modal editor. Everything is configurable through bind and the ~/.inputrc file.

Readline only exists in an interactive shell with a terminal, so the live examples below ask the shell about its bindings rather than pressing keys.

Terminal
bind -p            # list all bindings
bind -q kill-line  # which keys run kill-line?
set -o vi          # vi editing mode

Try it

Bash Asking Readline which keys do what

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Editing mode and Readline variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
bind [-m KEYMAP] -p | -P | -v | -V | -s | -S   # list bindings, variables, macros
bind -q FUNCTION                               # keys bound to a function
bind '"KEYSEQ": FUNCTION'                      # bind a key to a function
bind '"KEYSEQ": "text"'                        # bind a key to a macro
bind -x '"KEYSEQ": shell-command'              # run a shell command
set -o emacs | set -o vi                       # editing mode

Frequently used default bindings in emacs mode:

Keys Action
Ctrl+A, Ctrl+E Move to the start or end of the line.
Alt+B, Alt+F Move back or forward one word.
Ctrl+B, Ctrl+F Move back or forward one character.
Ctrl+U, Ctrl+K Cut from the cursor to the start, or to the end, of the line.
Ctrl+W, Alt+D Cut the word before, or after, the cursor.
Ctrl+Y Paste (yank) the most recently cut text.
Ctrl+_ or Ctrl+X Ctrl+U Undo the last edit.
Ctrl+R, Ctrl+S Search history backward or forward as you type.
Ctrl+P, Ctrl+N Previous and next history entry (same as the arrow keys).
Alt+. Insert the last argument of the previous command; press again for older ones.
Alt+# Comment out the line and enter it into history without running it.
Tab, Alt+?, Alt+* Complete, list possible completions, insert all completions.
Ctrl+L Clear the screen.
Ctrl+X Ctrl+E Edit the current line in $EDITOR; the saved text is run.
Ctrl+D Delete the character under the cursor, or exit on an empty line.

Returns: bind returns 0 on success and non-zero for an unknown function or invalid key sequence. Outside an interactive shell it prints a warning that line editing is not enabled.

Errors and edge cases: Ctrl+S may freeze the terminal in some setups because of XOFF flow control; Ctrl+Q resumes it, and stty -ixon disables the behavior.

Usage notes

  • Cut and paste ring. Text removed by the Ctrl+K, Ctrl+U and Ctrl+W family goes into a kill ring, not just a single buffer. After Ctrl+Y, Alt+Y cycles through older kills.
  • Alt on macOS. In Terminal.app and iTerm2 you may have to enable "Use Option as Meta key", or press Esc and then the letter instead.
  • Persistent changes go in ~/.inputrc, for example set completion-ignore-case on, set show-all-if-ambiguous on, or "\e[A": history-search-backward to make the Up arrow search by prefix. Put $include /etc/inputrc first to keep the system defaults.
  • bind in .bashrc works too, but is only applied to Bash; ~/.inputrc applies to every Readline program.
  • vi mode. Esc leaves insert mode; then h, l, w, b, 0, $, dd, cw, k, j and / work as in vi. Show the current mode in the prompt with set show-mode-in-prompt on.
  • History navigation: Ctrl+R with a partial command is faster than scrolling. Press Ctrl+R again to go to an older match, Enter to run it, or Esc to edit it first.
  • bind -x lets a key run a shell function. The line contents are in READLINE_LINE and the cursor position in READLINE_POINT, which you can modify.
  • Outside Bash. Zsh uses ZLE with similar defaults; Fish has its own system.