- Command Line Editing
- Using History
The goal of this chapter is to learn some bash features that make keyboard use faster and more efficient. This chapter will introduce the following commands:
clear- Clear the terminal screen
history- Display or manipulate 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. For example, the arrow keys move the cursor, but there are many more features.
Please note that some of the key sequences covered in this chapter (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.
Here is a list of keys used to move the cursor:
ctrl+A- Move cursor to the beginning of the line.
ctrl+E- Move the cursor to the end of the line.
ctrl+F- Move the cursor forward one character.
ctrl+B- Move the cursor backward one character.
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
clearcommand does the same thing.
Here is a list of keys that are used to edit characters:
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.
Here is a list of cut and paste commands:
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.
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.
The history facility is a useful resource for reducing the amount of typing you have to do, especially when combined with command line editing.
At any time, we can view and filter the contents of the command history list.
[user@linux ~]$ history | less [user@linux ~]$ history | grep /usr/bin
We can even use the history number and do another type of expansion called history expansion. Let’s say we want to get the contents of the 56th line in the history list.
[user@linux ~]$ !56
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
Here is a list of history expansion commands:
!!- Repeat the last command.
!number- Repeat history list item
!string- Repeat last history list item starting with
!?string- Repeat last history list item containing
Be caution against using the
!?string forms unless you are absolutely sure of the contents of the history list items.
To learn more about the history expansion you can read the “HISTORY EXPANSION” section of the bash man page.
In addition to the command history feature in bash, most Linux distributions include a program called script that can be used to record an entire shell session and store it in a 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.
In this chapter, we covered some of the keyboard tricks that the shell provides to help us reduce our workloads.