Advanced Keyboard Tricks
Chapter 08
Unix is often referred as "the preferred operating system for keyboard enthusiasts". The presence of a command line in Unix underscores this idea. Interestingly, despite being command line users, the preference is not for excessive typing. This is evident in the succinct names of many commands like cp, ls, mv, and rm. The command line culture values efficiency, aiming to accomplish tasks with minimal keystrokes. Embracing laziness, the primary aim is maximum output with minimal input. Moreover, the allure lies in never having to shift from the keyboard to the mouse. This chapter explores bash functionalities that enhance speed and efficiency in keyboard usage.
These commands are set to appear in this lesson:
clear - Clear the screen
history - Display the contents of the history list
Command Line Editing
Bash utilizes a library known as Readline, which integrates command line editing capabilities, as we've previously observed. While the arrow keys navigate the cursor, there's a plethora of additional features available. Consider these as supplementary tools for our tasks. It's not necessary to master them all, but plenty are highly beneficial. Select and adopt those that suit your needs.
Note
Certain key sequences outlined below, especially those involving the Alt key, might be intercepted by the GUI for alternate functions. However, when operating within a virtual console, all key sequences should function as intended.
Cursor Movement
The following table lists the keys used to move the cursor:
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
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
In the Readline documentation, the terms killing and yanking are used instead of the more common "cutting" and "pasting." When items are cut, they are stored in a buffer known as 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.
The Meta Key
Delving into the Readline documentation, located in the READLINE section of the bash manual, you'll encounter the term "meta key." On contemporary keyboards, this corresponds to the Alt key, but this wasn't always the case. In an era preceding personal computers but following Unix, not everyone owned a computer; some had terminals instead. Terminals were communication devices with a text display screen and a keyboard, linked typically via serial cable to a larger computer or its communication network. These terminals, varying in brands and keyboard configurations, functioned with diverse display features. Given the variation, software developers aimed for compatibility by writing applications based on the universal ASCII understanding. Unix systems developed sophisticated methods to manage terminals and their diverse display attributes. Considering the uncertainty of a dedicated control key on terminals, the developers of Readline invented one known as "meta." Although the modern Alt key functions as the meta key on keyboards today, using the Esc key, when operating on a terminal (which is still viable in Linux), replicates the effect of holding down the Alt key.
Completion
The shell offers assistance through a feature known as completion. This function activates when you press the tab key while entering a command. Let's explore its functionality. Consider a home directory structured like this:
Try typing the following but don't press the Enter key:
Now press the tab key:
Observe how the shell filled in the line? Let's attempt another one. Once more, refrain from pressing Enter just yet.
Press tab:
No completion occurred; instead, a beep sounded. This occurred because "D" matches multiple entries in the directory. Successful completion requires an unambiguous "clue" to be provided. Let's explore further:
Then press tab:
The completion is successful.
In this instance, we observed completion of pathnames, the most typical application. However, completion extends beyond pathnames; it functions on variables (when starting with $), user names (beginning with ~), commands (as the first word on the line), and hostnames (when initiated with @). Notably, hostname completion solely operates for hostnames documented in /etc/hosts.
Several key sequences, both control and meta, are linked to the completion feature:
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 are several more that I consider rather obscure. You can find a comprehensive list in the bash manual under the "READLINE" section.
Using History
As we explored in Chapter 1, bash retains a record of entered commands in a file named .bash_history within your home directory. This collection of commands serves as a valuable asset, significantly cutting down the amount of typing required, particularly when coupled with command line editing.
Searching History
At any time, we can view the contents of the history list by:
As a default setting, bash retains the most recent 500 commands you've input. We'll explore how to modify this value in a subsequent chapter. Suppose we aim to locate the commands used to list /usr/bin. One method to achieve this is:
Now, suppose within our results, we come across a line containing an intriguing command like this:
88 ls -l /usr/bin > ls-output.txt
The "88" signifies the line number of the command in the history list. We can swiftly utilize this using another form of expansion known as history expansion. To employ our discovered line, we could execute the following:
bash will interpret "!88" as the content of the eighty-eighth line within the history list. We'll delve into other types of history expansion shortly.
bash also offers the capability to incrementally search through the history list. This allows us to instruct bash to search the history list while entering characters, refining the search with each additional character. To initiate an incremental search, press Ctrl-r followed by the text you're seeking. Once found, you can press Enter to execute the command or Ctrl-j to copy the line from the history list to the current command line. To locate the next occurrence of the text (advancing "up" the history list), press Ctrl-r again. To stop the search, use either Ctrl-g or Ctrl-c. Here's a demonstration:
First press Ctrl-r:
The prompt alters to signify the ongoing reverse incremental search. It's termed "reverse" because we're searching from the current moment backward in time. Following that, we begin typing our search text, such as /usr/bin in this example:
Instantly, the search yields our desired result. With this outcome, we can execute the command by hitting Enter or copy it to our current command line for additional editing by pressing Ctrl-j. Let's opt to copy it. Press Ctrl-j:
Our shell prompt reappears, and our command line is prepped and ready for action!
Below is a table detailing several keystrokes utilized to manage the history list:
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 in 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 provides a distinct form of expansion for elements within the history list using the ! character. We've previously explored how the exclamation point followed by a number can insert an entry from the history list. There are several other expansion functionalities available:
!!
Repeat the last command. It is probably easier to press 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'd advise exercising caution when employing the !string and !?string formats unless you're entirely certain about the contents of the history list items.
There are numerous other elements within the history expansion mechanism, but delving deeper might be overly complex, and our heads could potentially explode if we persist. For a comprehensive understanding, refer to the HISTORY EXPANSION section in the bash manual; it covers all the intricate details. Feel free to explore further!
script
scriptAside from the command history feature within bash, the majority of Linux distributions incorporate a program named script, which captures and saves an entire shell session into a file. The command's fundamental syntax is:
Here, file represents the designated name for storing the recording. If no file is specified, the default file typescript is used. For a comprehensive overview of the program's options and functionalities, refer to the script man page.
Summary
Throughout this chapter, we've delved into various keyboard techniques offered by the shell, aimed at aiding dedicated typists in lightening their workloads. I anticipate that as you spend more time navigating the command line, you might revisit this chapter to acquire additional tricks. At this stage, regard them as optional but potentially advantageous.
Last updated