A keybinding is simply the mapping of a specific key combination to an action.
Keybindings that we are all familiar with outside of the command line environment are things like:
- Control-c: copies selected text
- Control-v: pastes copied text
In this tutorial, we’ll take a look at some of the default keybindings provided in the command line environment (and also where to look if you forget one).
There are many keybindings available by default when working in the Linux command line to make navigation and editing much easier. They are powered by a tool called Readline.
Here are few I often use (If you are familiar with the Emacs editor, you will likely see similarities):
The following keybindings are used for movements:
|Control-a||Position cursor at the start of the line|
|Control-e||Position the cursor after the last character of the line|
|Control-f||Move the cursor forward by one character (same behaviour as the arrow key)|
|Control-b||Move the cursor backwards by one character (same behaviour as the arrow key)|
The following keybindings are used for deletions:
|Control-k||Delete all text after (and including) cursor|
|Control-u||Delete all text before (not including) cursor|
|Control-w||Delete immediate WORD before cursor (a WORD is a sequence of any consecutive non-whitespace characters)|
|Control-h||Delete char before cursor (think backspace)|
|Control-d||When text exist => delete char under cursor (think delete). When no text => exit shell|
The following keybindings are used for history:
|Control-p||Cycle to previous command|
|Control-n||Cycle to next command|
|Control-r||Reverse index search (allows you to type part of a command. searches in reverse order through command history for a matching command)|
The following are few misc keybindings:
|Control-l||Clear display (similar to ‘clear’ command)|
|Control-j||Execute current command (same as Enter/Return)|
|Control-c||Cancel current command, return empty prompt|
|Control-/||Redo the last undone edit|
|Control-?||Undo the last edit|
To see a full list currently bound and available keybindings and functions use the ‘bind’ command (this is a good quick reference when you forget one):
$ bind -p
You’ll see output that looks something like this:
"\C-g": abort "\C-x\C-g": abort "\e\C-g": abort "\C-j": accept-line …
The format here is:
key combination: binding
Please note that you’ll use the same format to define your own custom keybindings.
Remember that you can use grep to find the bindings you are interested in.
For example, to see only the binding that use the Control key:
$ bind -p | grep '\\C'
Note: In the above command, I’ve got to escape the backslash (\) character in the grep string.