8. Terminal Agility

When you know a few tricks, working inside a terminal can be a very fast way of operating. Alas, even experienced people don’t know a lot of these ways, and end up not being as productive as they could be. So in this section, I aim to increase your agility in the terminal by giving you a few exercises for you to practice in the following labs.

You do not need to memorise this list; but if you never hear of them, you may never think of them. The most important ones I have marked with a ; it would pay to memorise these few to begin with.

Most shells use a library called readline; since this is widely available on different systems, these keystroke sequences will work in many shells, on Linux as well as Mac.

Important

The following list uses the Emacs style of keystoke notation, so C-a means to type a while holding down the Ctrl key.

M-a means to hold down the Meta key, which is not a key you will find on modern keyboards; instead, use the Left Alt (or the Left Option key on a Mac).

Cursor movement

C-a, C-e

Move to the beginning and end of the line. Type some text in your shell, use the keystrokes to move to the beginning and end of the line.

M-f, M-b

Move forward and back a word at a time, but note that this doesn’t always work; the keystroke might instead invoke menu functions instead. Thankfully, some Linux systems, Ubuntu included, define Ctrl+LeftArrow and Ctrl+RightArrow for the same task.

Editing commands

Backspace, C-h

Delete character to the left of the cursor. Knowing about C-h can be useful, because Backspace is not always reliable on some systems.

Del, C-d

Delete character to the right of the cursor.

C-w

Delete the word to the left of the cursor. Type a few words in the shell, then use C-w to erase them, word by word. This is much faster than using backspace. It can be faster to erase and retype a whole word than to use backspace.

C-c

Abort the current command being entered. Begin typing a command, and then type C-c, to see it disappear.

C-l

Clear screen. This commonly also means redraw screen in various full-screen terminal applications, such as editors.

History commands

C-r

Start searching backwords in your history, incrementally.

For example, assume you had in your command history a command that mentioned a file dhcpd.conf. You can then type C-r and then start typing dhcpd.conf, as you type each character, you will be shown items in your history that match. You can type C-r again to show matches earlier in your history.

You can use the standard editing keystrokes to edit the command if you like, or you could just type Enter to run the command again.

!!

(pronouced “bang-bang”) is a sequence that you will probably use very often, as it expands to the last command line you used. This is useful when you forget to use sudo:

$ some long admin command
Operation not permitted       Oops, forgot to use sudo
$ sudo !!
sudo some long admin command
Now it works, and we saved a lot of typing or navigating our history.

Command Completion

Tab Completion

One incredible time-saving device I see a lot of students not using is Tab completion. This makes it much faster to accurately navigate around the filesystem and commands.

To use Tab-completion, you simply type Tab, and it will complete as much your current word as it can before it gets ambiguous. If you type Tab again, it will present you with a list of choices. If there is a large number of choices, you will be prompted with a --More-- prompt, and you can press Space to view the next page, or q to quit. The best way is simply to try it out. Don’t be afraid, you have a lot to gain.

C-x C-e

This one is perhaps less useful for a beginner, but if you remember it, it will make developing longer commands (often the precursor to a script) much easier. This keystroke sequence will open your current command in your preferred editor (which can be set using the EDITOR environment variable, which by default on Ubuntu is nano).