CIS120Linux Fundementals
Keyboard Tricks
Command line users don't enjoy excessive typing, which is why many commands have short names like cp
, ls
, mv
, and rm
. One of the cherished goals of using the command line is to achieve the most with the fewest keystrokes, minimizing the need to use a mouse. This chapter explores bash features that enhance keyboard efficiency.
Command Line Editing
The bash
shell uses a library called Readline to implement command line editing, offering features beyond basic arrow key navigation. These tools, while numerous, can be selectively learned to increase productivity. Note that some key sequences, especially those involving the Alt key, may be intercepted by the GUI but will work correctly in a virtual console.
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 right arrow key). |
Ctrl-b |
Move cursor backward one character (same as 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. |
Modifying Text Commands:
Key | Action |
---|---|
Ctrl-d |
Delete the character at the cursor location. |
Ctrl-t |
Transpose the character at the cursor with the preceding one. |
Alt-t |
Transpose the word at the cursor with the preceding word. |
Alt-l |
Convert characters from the cursor to the end of the word to lowercase. |
Alt-u |
Convert characters from the cursor to the end of the word to uppercase. |
Cutting and Pasting (Killing and Yanking) Text:
Readline documentation refers to cutting and pasting as killing and yanking. Cut items are stored in a temporary buffer called the kill-ring.
Key | Action |
---|---|
Ctrl-k |
Kill text from the cursor to the end of the line. |
Ctrl-u |
Kill text from the cursor to the beginning of the line. |
Alt-d |
Kill text from the cursor to the end of the current word. |
Alt-Backspace |
Kill text from the cursor to the beginning of the current word (or previous word if at the start of a word). |
Ctrl-y |
Yank text from the kill-ring and insert it at the cursor location. |
The Meta Key
The term meta key, often used in Readline documentation, maps to the Alt key on modern keyboards. Historically, before the widespread use of PCs, terminals connected to larger computers used a key designated as meta. On modern systems, pressing the Esc key can substitute for holding the Alt key.
Completion
The shell provides a completion mechanism using the Tab key. Completion simplifies the process of typing commands by automatically filling in pathnames, variables, usernames, commands, and hostnames.
Examples:
For completion to be successful, the input must be unambiguous. For example let's say you have mutiple folders in the same directory that start with dir (dir1, dir2 and dir3). If you enter the line below and press the tab key once you will hear a chime and on the second press it will give you all the folders (or files) that start with dir
First tab press
[me@linuxbox ~]$ ls dir
Second tab press
[me@linuxbox ~]$ ls dir
dir1/ dir2/ dir3
If you have 100 or more folders or files that start with 0 (000 - 099) and you enter 0 and press tab. You will hear a chime and then the following will be displayed
Display all 100 possibilites? (y or n)
This is the behavior on the cidermill server.
Completion commands:
Key | Action |
---|---|
Alt-? |
Display a list of possible completions. |
Alt-* |
Insert all possible completions. |
Using History
bash
maintains a history of entered commands, stored in the .bash_history
file in the home directory. This feature, combined with command line editing, reduces typing effort.
To view the history list:
[me@linuxbox ~]$ history | less
To search the history list for commands:
[me@linuxbox ~]$ history | grep /usr/bin
Using history expansion:
[me@linuxbox ~]$ !88
History Expansion Commands:
Sequence | Action |
---|---|
!! |
Repeat the last command. |
!number |
Repeat history list item number. |
!string |
Repeat last history list item starting with string. |
!?string |
Repeat last history list item containing string. |
script Command
The script
command in Linux is used to record a terminal session, capturing all the input and output during the session into a file. It is particularly useful for creating logs of interactive shell sessions, debugging, or documenting command-line activities.
Syntax
script [options] [file]
Key Features
Records Terminal Sessions
Captures everything displayed in the terminal, including user input and command output.Default Output File
If no file is specified, the session is saved in a file namedtypescript
.Interactive Use
The command runs interactively and exits when the user typesexit
or pressesCtrl+D
.
Common Options
Option | Description |
---|---|
-a |
Append output to the specified file instead of overwriting it. |
-c <command> |
Run a specific command and log its output. The session ends after completion. |
-f |
Flush output to the file as it is written, useful for real-time monitoring. |
-q |
Run in quiet mode, suppressing the start and end messages. |
--timing=<file> |
Record timing information for playback with the scriptreplay command. |
Examples
Basic Use
script session.log
This starts recording the session into session.log
.
Appending to a File
script -a session.log
Appends the current session to an existing log file.
Running a Command
script -c "ls -l" output.log
Logs the output of ls -l
into output.log
.
Quiet Mode
script -q log.txt
Runs the session without displaying the starting and ending messages.
Using Timing
script --timing=timing.log session.log
Creates a timing file timing.log
to replay the session later.
Replay the Session
The scriptreplay
command can be used to replay a session recorded with timing information:
scriptreplay timing.log session.log
Notes
- The
script
command is part of theutil-linux
package, so it is available on most Linux distributions. - It can capture all terminal activity, making it useful for auditing, troubleshooting, and education.
Summing Up
In this chapter, we've explored various keyboard tricks and features in bash
that can significantly reduce typing effort. These tools are optional but can be very helpful as you become more familiar with the command line.