Linux Keyboard Tricks

Working on Linux is all about Command Line with very minimal use of mouse. Having said that while working on Linux professionally your goal should be never having to lift your fingers from the keyboard and reach for the mouse.

You should know all the keyboard tricks which normally would require a mouse and graphical interface such as copying/deleting/modifying a text/line etc or many other day to system admin related tasks.

In this article I will try to cover some of the Linux related keyboard tricks which can help you with day to activities.


Command Line Editing

bash uses a library (a shared collection of routines that different programs can use) called Readline to implement command line editing. We know, for example, that the arrow keys move the cursor, but there are many more features. Think of these as additional tools that we can employ in our work. It’s not important to learn all of them, but many of them are very useful. Pick and choose as desired.

Ctrl+A Move cursor to the beginning of the line.

CTRL+E Move cursor to the end of the line.

CTRL+F Move cursor forward one character; same as the right arrow key.

CTRL+B Move cursor backward one character; same as the left arrow key.

ALT+F Move cursor forward one word.

ALT+B Move cursor backward one word.

CTRL+L Clear the screen and move the cursor to the top-left corner. The clear command does the same thing.


Modifying Text

Because it’s possible we might make a mistake while composing commands, we need a way to correct them efficiently. Below keyboard combinations are used to edit characters on the command line.

CTRL+D Delete the character at the cursor location.

CTRL+T Transpose (exchange) the character at the cursor location with the one preceding it.

ALT+T Transpose the word at the cursor location with the one preceding it.

ALT+L Convert the characters from the cursor location to the end of the word to lowercase.

ALT+U Convert the characters from the cursor location to the end of the word to uppercase.


Cutting and Pasting (Killing and Yanking) Text

The Readline documentation uses the terms killing and yanking to refer to what we would commonly call cutting and pasting. Items that are cut are stored in a buffer (a temporary storage area in memory) called the kill-ring.

CTRL+K Kill text from the cursor location to the end of line.

CTRL+U Kill text from the cursor location to the beginning of the line.

ALT+D Kill text from the cursor location to the end of the current word.

ALT+BACKSPACE Kill text from the cursor location to the beginning of the current word. If the cursor is at the beginning of a word, kill the previous word.

CTRL+Y Yank text from the kill-ring and insert it at the cursor location.



Another way that the shell can help you is through a mechanism called completion. Completion occurs when you press the TAB key while typing a command. Let’s see how this works. Given a home directory that looks like this:

[me@linuxbox ~]$ ls
Desktop    ls-output.txt  Pictures  Templates      Videos
Documents  Music          Public

try typing the following, but don’t press ENTER:

[me@linuxbox ~]$ ls l

Now press TAB.

[me@linuxbox ~]$ ls ls-output.txt

See how the shell completed the line for you? Let’s try another one. Again, don’t press ENTER.

[me@linuxbox ~]$ ls D

Press TAB.

[me@linuxbox ~]$ ls D

No completion, just nothing. This happened because D matches more than one entry in the directory. For completion to be successful, the “clue” you give it has to be unambiguous. If we go further, as with the following:

[me@linuxbox ~]$ ls Do

and then press TAB:

[me@linuxbox ~]$ ls Documents

the completion is successful.

While this example shows completion of pathnames, which is its most common use, completion will also work on variables (if the beginning of the word is a $), usernames (if the word begins with ~), commands (if the word is the first word on the line), and hostnames (if the beginning of the word is @). Hostname completion works only for hostnames listed in /etc/hosts.

ALT-? Display a list of possible completions. On most systems, you can also do this by pressing the TAB key a second time, which is much easier.

ALT-* Insert all possible completions. This is useful when you want to use more than one possible match.


Using History

bash maintains a history of commands that have been entered. This list of commands is kept in your home directory in a file called bash_history. The history facility is a useful resource for reducing the amount of typing you have to do, especially when combined with command line editing.


Searching History

At any time, we can view the contents of the command history list by doing the following:

[me@linuxbox ~]$ history | less

By default, bash stores the last 500 commands we have entered, though most modern distributions set this value to 1,000. We will see how to adjust this value in Chapter 11. Let’s say we want to find the commands we used to list /usr/bin. This is one way we could do this:

[me@linuxbox ~]$ history | grep /usr/bin

And let’s say that among our results we got a line containing an interesting command like this:

88 ls -l /usr/bin > ls-output.txt

The 88 is the line number of the command in the history list. We could use this immediately using another type of expansion called history expansion. To use our discovered line, we could do this:

[me@linuxbox ~]$ !88

bash will expand !88 into the contents of the 88th line in the history list.

bash also provides the ability to search the history list incrementally. This means we can tell bash to search the history list as we enter characters, with each additional character further refining our search. To start incremental search, press CTRL+R followed by the text you are looking for. When you find it, you can either press ENTER to execute the command or press CTRL+J to copy the line from the history list to the current command line. To find the next occurrence of the text (moving “up” the history list), press CTRL+R again. To quit searching, press either CTRL+G or CTRL+C. Here we see it in action:

[me@linuxbox ~]$

First press CTRL+R.


The prompt changes to indicate that we are performing a reverse incremental search. It is “reverse” because we are searching from “now” to sometime in the past. Next, we start typing our search text. In this example, we’re searching for /usr/bin:

(reverse-i-search)`/usr/bin': ls -l /usr/bin > ls-output.txt

Immediately, the search returns our result. With our result, we can execute the command by pressing ENTER, or we can copy the command to our current command line for further editing by pressing CTRL+J. Let’s copy it. Press CTRL+J.

[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt


Our shell prompt returns, and our command line is loaded and ready for action!

CTRL+P Move to the previous history entry. This is the same action as the up arrow.

CTRL+N Move to the next history entry. This is the same action as the down arrow.

ALT-< Move to the beginning (top) of the history list. ALT-> Move to the end (bottom) of the history list, i.e., the current command line.

CTRL+R Reverse incremental search. This searches incrementally from the current command line up the history list.

ALT+P Reverse search, nonincremental. With this key, type in the search string and press ENTER before the search is performed.

ALT+N Forward search, nonincremental.

CTRL+O Execute the current item in the history list and advance to the next one. This is handy if you are trying to re-execute a sequence of commands in the history list.


History Expansion

The shell offers a specialized type of expansion for items in the history list by using the ! character. We have already seen how the exclamation point can be followed by a number to insert an entry from the history list.

! Repeat the last command. It is probably easier to press the up arrow and ENTER.

!number Repeat history list item number.

!string Repeat last history list item starting with string.

!?string Repeat last history list item containing string.


I caution against using the !string and !?string forms unless you are absolutely sure of the contents of the history list items.


Advanced Keyboard Tricks

GoLinuxCloud Logo

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages