≡ Menu

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

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.

Add your comment

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

Comments on this entry are closed.

  • Jalal Hajigholamali May 13, 2014, 3:04 am

    Hi,

    Thanks a lot
    very useful and nice article…

  • Geoff May 13, 2014, 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

  • Mike May 13, 2014, 6:34 am

    I’ve liked this 🙂
    thank you, TGS

  • rahul singh May 13, 2014, 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

  • zhiming May 13, 2014, 8:28 am

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

  • Jthan24 May 13, 2014, 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!!

  • Lev Lafayette May 14, 2014, 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.

  • bama May 18, 2014, 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. 🙂

  • Rahul Singh May 22, 2014, 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.

  • Spaldam May 24, 2014, 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.

  • MRoeling May 30, 2014, 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