Advanced Keyboard Tricks - Learning the Shell - The Linux Command Line (2012)

The Linux Command Line (2012)

Part I. Learning the Shell

Chapter 8. Advanced Keyboard Tricks

I often kiddingly describe Unix as “the operating system for people who like to type.” Of course, the fact that it even has a command line is a testament to that. But command line users don’t like to type that much. Why else would so many commands have such short names, like cp, ls, mv, and rm?

In fact, one of the most cherished goals of the command line is laziness—doing the most work with the fewest keystrokes. Another goal is never having to lift your fingers from the keyboard—never reaching for the mouse. In this chapter, we will look at bash features that make keyboard use faster and more efficient.

The following commands will make an appearance:

· clear—Clear the screen.

· history—Display the contents of the history list.

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 have already seen some of this. 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.


Note

Some of the key sequences below (particularly those that use the alt key) may be intercepted by the GUI for other functions. All of the key sequences should work properly when using a virtual console.


Cursor Movement

Table 8-1 lists the keys used to move the cursor.

Table 8-1. Cursor Movement Commands

Key

Action

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

Table 8-2 lists keyboard commands that are used to edit characters on the command line.

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. Table 8-3 lists the commands for cutting and pasting. Items that are cut are stored in a buffer called the kill-ring.

Table 8-2. Text Editing Commands

Key

Action

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.

Table 8-3. Cut and Paste Commands

Key

Action

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.


The Meta Key

If you venture into the Readline documentation, which can be found in the “READLINE” section of the bash man page, you will encounter the term meta key. On modern keyboards this maps to the alt key, but it wasn’t always so.

Back in the dim times (before PCs but after Unix) not everybody had their own computer. What they might have had was a device called a terminal. A terminal was a communication device that featured a text-display screen and a keyboard and had just enough electronics inside to display text characters and move the cursor around. It was attached (usually by serial cable) to a larger computer or the communication network of a larger computer. There were many different brands of terminals, and they all had different keyboards and display feature sets. Since they all tended to at least understand ASCII, software developers wanting portable applications wrote to the lowest common denominator. Unix systems have a very elaborate way of dealing with terminals and their different display features. Since the developers of Readline could not be sure of the presence of a dedicated extra control key, they invented one and called it meta. While the alt key serves as the meta key on modern keyboards, you can also press and release the esc key to get the same effect as holding down the alt key if you’re still using a terminal (which you can still do in Linux!).


Completion

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. Say your home directory looks like this:

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

Try typing the following but don’t press the enter key:

[me@linuxbox ˜]$ ls l

Now press the tab key:

[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 a beep. 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. We can go further:

[me@linuxbox ˜]$ ls Do

Then press tab :

[me@linuxbox ˜]$ ls Documents

The completion is successful.

While this example shows completion of pathnames, which is completion’s 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.

A number of control and meta key sequences are associated with completion (see Table 8-4).

Table 8-4. Completion Commands

Key

Action

alt-?

Display 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.

There quite a few more that I find rather obscure. You can see a list in the bash man page under the “READLINE” section.


Programmable Completion

Recent versions of bash have a facility called programmable completion. Programmable completion allows you (or, more likely, your distribution provider) to add additional completion rules. Usually this is done to add support for specific applications. For example, it is possible to add completions for the option list of a command or match particular file types that an application supports. Ubuntu has a fairly large set defined by default. Programmable completion is implemented by shell functions, a kind of mini shell script that we will cover in later chapters. If you are curious, try

set | less

and see if you can find them. Not all distributions include them by default.


Using History

As we discovered in Chapter 1, 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 history list:

[me@linuxbox ˜]$ history | less

By default, bash stores the last 500 commands you have entered. 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. Here 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 number 88 is the line number of the command in the history list. We could use this immediately with 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. We will cover other forms of history expansion a little later.

bash also provides the ability to search the history list incrementally. This means that we can tell bash to search the history list as we enter characters, with each additional character further refining our search. To start an incremental search, enter 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:

(reverse-i-search)`':

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

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

Immediately, the search returns its result. Now 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!

Table 8-5 lists some of the keystrokes used to manipulate the history list.

Table 8-5. History Commands

Key

Action

ctrl-P

Move to the previous history entry. Same action as the up arrow.

ctrl-N

Move to the next history entry. 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. Searches incrementally from the current command line up the history list.

alt-P

Reverse search, non-incremental. With this key, type the search string and press enter before the search is performed.

alt-N

Forward search, non-incremental.

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. There are a number of other expansion features (see Table 8-6).

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

Many more elements are available in the history expansion mechanism, but this subject is already too arcane and our heads may explode if we continue. The History Expansion section of the bash man page goes into all the gory details. Feel free to explore!

Table 8-6. History Expansion Commands

Sequence

Action

!!

Repeat the last command. It is probably easier 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.


script

In addition to the command history feature in bash, most Linux distributions include a program called script, which can be used to record an entire shell session and store it in a file. The basic syntax of the command is

script [file]

where file is the name of the file used for storing the recording. If no file is specified, the file typescript is used. See the script man page for a complete list of the program’s options and features.


Final Note

In this chapter we have covered some of the keyboard tricks that the shell provides to help hardcore typists reduce their workloads. I suspect that as time goes by and you become more involved with the command line, you will refer to this chapter to pick up more of these tricks. For now, consider them optional and potentially helpful.