How to Navigate Linux Command Line Using Keybinding (Linux Ctrl Command Examples)

by Aaron Tabor on May 12, 2014

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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 11 comments… read them below or add one }

1 Jalal Hajigholamali May 13, 2014 at 3:04 am

Hi,

Thanks a lot
very useful and nice article…

2 Geoff May 13, 2014 at 5:08 am

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

3 Mike May 13, 2014 at 6:34 am

I’ve liked this :-)
thank you, TGS

4 rahul singh May 13, 2014 at 7:23 am

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

5 zhiming May 13, 2014 at 8:28 am

Alt+f, move forward one word
Alt+b,move backwork one word.

6 Jthan24 May 13, 2014 at 9:08 am

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!!

7 Lev Lafayette May 14, 2014 at 12:44 am

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.

8 bama May 18, 2014 at 4:19 am

Very nice summary, but I have
set -o vi
in my .bashrc to enjoy vi-style editing in the command line instead. :-)

9 Rahul Singh May 22, 2014 at 5:56 am

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.

10 Spaldam May 24, 2014 at 5:13 pm

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.

11 MRoeling May 30, 2014 at 12:32 am

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

Leave a Comment

Previous post:

Next post: