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.
bind -p # list all bindings bind -q kill-line # which keys run kill-line? set -o vi # vi editing mode
Try it
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.
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
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+UandCtrl+Wfamily goes into a kill ring, not just a single buffer. AfterCtrl+Y,Alt+Ycycles through older kills. Alton macOS. In Terminal.app and iTerm2 you may have to enable "Use Option as Meta key", or pressEscand then the letter instead.- Persistent changes go in
~/.inputrc, for exampleset completion-ignore-case on,set show-all-if-ambiguous on, or"\e[A": history-search-backwardto make the Up arrow search by prefix. Put$include /etc/inputrcfirst to keep the system defaults. bindin.bashrcworks too, but is only applied to Bash;~/.inputrcapplies to every Readline program.- vi mode.
Escleaves insert mode; thenh,l,w,b,0,$,dd,cw,k,jand/work as in vi. Show the current mode in the prompt withset show-mode-in-prompt on. - History navigation:
Ctrl+Rwith a partial command is faster than scrolling. PressCtrl+Ragain to go to an older match,Enterto run it, orEscto edit it first. bind -xlets a key run a shell function. The line contents are inREADLINE_LINEand the cursor position inREADLINE_POINT, which you can modify.- Outside Bash. Zsh uses ZLE with similar defaults; Fish has its own system.