Bash shell: default keyboard shortcuts


We are going to enumerate default keyboard shortcuts for bash shell.


Bash obtains most of its behavior from GNU Readline and GNU History libraries.


Emacs vs vi key combinations


Readline comes with emacs like keybindings by default.

In this document we will explain emacs like commands.


You could also set vi like mode in the init file.

Keybindings can be customized in Readline init file (/etc/inputrc)


GNU Readline and History libraries


Applications linked to GNU Readline library provide the user with editing capabilities for command lines as they are typed in.

GNU Readline also provides history functions, based on History library.


GNU History library can be used separately from Readline.


Keyboard Shorcuts


Nomenclature:

We will use this convention. E.g:

C-e means press Control and e key together.

M-e means press meta key and e. We could simulate metakey by first pressing Esc key and afterwards letter e (metafying e key).

M-C-k means metafying C-k , i.e: first pressing ESC and afterwards C-k.


Self insert

(a, b, A, 1, !, ...)

Those characters and some others are inserted when its associated key is pressed.

E.g: e prints an 'e'


Accept line

RET accept a line independently of where cursor position is.


Special symbols

C-i equals Tab

C-j equals newline character

C-m equals Enter

C-d if pointer is at the beginning of the line and there are no other characters, this symbol equals END of FILE (EOF).


Quoted insert

C-v (or C-q) quoted insert

If we want to insert C-e instead of executing command associated to C-e we press:
C-v C-e

or to insert C-v
C-v C-v


NOTE: C-s and C-q are restricted to terminal control flow if XON/XOFF is enabled.
http://unix.stackexchange.com/questions/72086/ctrl-s-hang-terminal-emulator


Move the cursor

C-b Move back one character

C-f Move forward one character


C-a Move to beggining of the line

C-e Move to the end of the line


M-f Move forward one word

M-b Move backward one word.



Delete characters

DEL or Backspace Delete character to the left of the cursor

C-d Delete character under the cursor

M-DEL Delete word before cursor.

M-d Delete word after the cursor.


Clear the screen

C-l clear the screen and reprint current line at the top.


Changing text

M-u upcase from cursor to end of current word.

M-l downcase from cursor to end of current word.

C-t transpose characters

M-t transpose words

M-c capitalize word (from cursor to end of current word)


Killing and Yanking commands

C-k kill text from cursor to end of the line.

C-u kill from cursor to beginning of current line.

M-d kill text from cursor to the end of the word.

M-DEL kill text from cursor to start of current word.

C-w kill from cursor to previous whitespace.

C-y yank back previously killed text.

M-y rotate kill ring and yank the new top.


Undo commands

C-x C-u Undo last editing command.

C-_ Undo last editing command.

M-r Undo all changes made to this line (revert line).


Abort

C-g Abort current editing command (Also used to stop an incremental search)


Point and mark

C-@ set the mark to the position of the point.

C-x C-x exchange point and mark positions.


Passing arguments to readline commands

We pass a numeric argument using meta key.


E.g: move forward 3 characters

M-3 C-f


Eg:

M-3 w print www


We can also pass negative numbers:

If we first press minus sign argument becomes negative.

E.g:

M-- C-k kill text to the start of the line.

M--3 C-f move backward 3 characters.


Editor

C-x C-e invoke an editor for current command line.


Auto-completion

TAB Attempt to perform completion on the text before point. The actual completion performed is application-specific.

Bash attempts completion treating the text as:
  • a variable(if the text begins with `$')

  • username (if the text begins with `~')

  • hostname (if the text begins with `@')

  • command (including aliases and functions)

  • If none of these produces a match, filename completion is attempted.



M-? list possible completions.

C-x / show possible filename completions.

M-/ perform filename completion.

C-x ~ show possible username completions.

M-~ perform username completion.

C-x $ show possible shell variable completions.

M-$ perform shell variable completion.

C-x @ show possible hostname completions.

M-@ perform hostname completion.

C-x ! show possible completions treating text before point as a command name. It tries to match against aliases, reserved words, shell functions, etc.

M-! perform command completion.

M-TAB perform completion against command history.


Keyboard macros

C-x ( start keyboard macro

C-x ) end keyboard macro

C-x e execute last defined keyboard macro.


Searching in the history

C-r search backward (incremental) in the history matching the search string typed so far.

C-s search forward (incremental) in the history. (NOTE: if you have sofware flow control enabled you will hang up your terminal, press C-q to recover)

http://unix.stackexchange.com/questions/72086/ctrl-s-hang-terminal-emulator


ESC and C-j stop current incremental search maintaining current search result.

C-g stops incremental search and restores original value.

RET execute current search result.


M-p search backward (non-incremental)

M-n search forward (non-incremental)


History expansion (From GNU History)

M-> Move to first line in history.

M-< Move to end of the input history (usually line currently being entered)


C-p previous command in history

C-n next command in history


!! execute again previous command

!3 execute again command line 3

!-n execute command n lines back
!! equals ( !-1 )

!string execute a command that starts with string

!?string execute a command that ends with string

!?string? execute a command that contains string


Using history word designators and modifiers we can substitute part of former commands and arguments before executing them again.
http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/


Bash Job control

C-z suspend current job (Send SIGSTOP to it)

C-y delayed suspension of current job (it continues until input is produced)


Signals

C-c send SIGINT to current job (Usually closes cleanly the process)


Customize init file and reread it.

Readline init file: /etc/inputrc

C-x C-r reread Readline init file.



REFERENCE


Bash reference manual:
https://www.gnu.org/software/bash/manual/bashref.html
https://www.gnu.org/software/bash/manual/bashref.html#Command-Line-Editing

GNU Readline page:
http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html

Readline Documentation:
http://cnswww.cns.cwru.edu/php/chet/readline/readline.html

Documentation for GNU History library:
http://cnswww.cns.cwru.edu/php/chet/readline/history.html

https://en.wikipedia.org/wiki/GNU_Readline