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):
Movement Keybindings
The following keybindings are used for movements:
Keybinding | Description |
---|---|
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) |
Deletion Keybindings
The following keybindings are used for deletions:
Keybinding | Description |
---|---|
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 |
History Keybindings
The following keybindings are used for history:
Keybinding | Description |
---|---|
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) |
Misc Keybindings
The following are few misc keybindings:
Keybinding | Description |
---|---|
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.
Comments on this entry are closed.
Hi,
Thanks a lot
very useful and nice article…
Nice intro.
The complete picture, however, is that all keystrokes are processed by stty before the shell gets to see them, so the likes of Ctrl-U, Ctrl-W are processed by stty. Similarly, although Ctrl-S is bound, stty uses it to do XON/XOFF signals, so it doesn’t work. (It “freezes” the output to the screen until you do Ctrl-Q.) I find the whole topic of processing keystrokes (including termcap/terminfo settings) very confusing!
You can test what stty processes by doing “set +o emacs” (or whatever keymap you are currently using) – this tells the shell to disable the editing mode. Now do some typing and try some Ctrl-U, Ctrl-W, etc keystrokes.
To see what stty has “taken charge of”, type “stty -a”.
Cheers,
Geoff
I’ve liked this 🙂
thank you, TGS
undo last edit does not work
cntrl-?
[alpha@localhost abc]$ lsb_release -a
LSB Version: :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3.1-amd64:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Release: 5.4
Codename: Tikanga
Alt+f, move forward one word
Alt+b,move backwork one word.
Nice article, but i have a other Ctrl command is Ctrl+z to block the active execution, the way to continue the process is with kill -CONT ID_PROCESS to continue the executions without blocking.
Thanks for the article very nice, have a nice day!!
It probably should be mentioned that many of these are shortcuts in Emacs mode, not Linux shortcuts as such (although they are widely implemented by default). One could use vi bindings instead e.g.: here and here.
Very nice summary, but I have
set -o vi
in my .bashrc to enjoy vi-style editing in the command line instead. 🙂
Great article.But I tried below shortcut which does not work on my system.
Control-?(Undo the last edit) while Control-/ (Redo the last undone edit) works fine.
For those used to VI religiously (and think holding down CTRL while simultaneously pressing another keys is akin to finger yoga) you can instead run this command in the bash shell:
set -o vi
And then use VI keystrokes to edit the command-line and search the history.
There are alse several usefull Alt keybindings.
Think of:
Alt-B: move one word to the left (Back)
Alt-F: move one word to the right (Forward)
And more: here