Linux command line 6 - Advanced Keyboard Tricks

LHT

This article mainly references the book The Linux Command Line (2nd Edition). The shell tool used is Xshell, and the operating system is CentOS 7.6. This means that some shortcuts may not work in other shell tools, and there may be slight differences in files across different Linux operating systems. Please analyze based on your actual situation.

Since CentOS 7.6 is EOL, consider using Rocky Linux or Alma Linux instead.

Advanced Keyboard Operations

Command Line Editing

Bash uses a library called Readline (a collection of shared routines that can be used by different programs) to implement command line editing.

💡 Some of the key combinations mentioned below (especially those using the Alt key) may not work or may trigger other functions due to conflicts with system shortcuts.

Moving the Cursor

Key Action
Ctrl-a Move the 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; same as the right arrow key.
Ctrl-b Move the cursor backward one character; same as the left arrow key.
Alt-f Move the cursor forward one word.
Alt-b Move the cursor backward one word.
Ctrl-l Clear the screen and move the cursor to the top left corner.
clear command Performs the same function.

Modifying Text

Key Action
Ctrl-d Delete the character at the cursor position.
Ctrl-t Swap the character at the cursor position with the character before it.
Alt-t Swap the word at the cursor position with the word before it.
Alt-l Convert the characters from the cursor position to the end of the word to lowercase.
Alt-u Convert the characters from the cursor position to the end of the word to uppercase.

Cutting and Pasting Text

In Readline documentation, the terms killing and yanking refer to what we commonly call cutting and pasting. The text that is cut is stored in a buffer called the kill-ring.

Key Action
Ctrl-k Cut the text from the cursor position to the end of the line.
Ctrl-u Cut the text from the cursor position to the beginning of the line.
Alt-d Cut the text from the cursor position to the end of the word.
Alt-Backspace Cut the text from the cursor position to the beginning of the word. If the cursor is at the start of a word, cut the previous word.
Ctrl-y Paste the text from the kill-ring at the cursor position.

Meta Key:
If you delve into Readline documentation, you may encounter the term “meta key” in the READLINE section of the bash manual. In modern keyboards, this meta key refers to the Alt key, but this is not always the case.

Unix systems have a clever way of handling various terminal products and their different display features. Since the developers of the Readline program could not guarantee the existence of a dedicated extra control key, they invented a control key called “meta.” However, on modern keyboards, the Alt key serves as the meta key. If you’re still using a terminal (you can still get one in Linux), you can also achieve the same effect by pressing and releasing the Esc key instead of the Alt key.

Autocompletion

Shell also enhances your efficiency through an autocompletion mechanism. When typing a command, pressing the Tab key triggers the completion feature. For example, consider the following directory:

1
2
[root@lht example]# ls
Desktop Documents ls-output.txt Music Pictures Public Templates Videos

If you type part of a command without pressing Enter:

1
[root@lht example]# ls l

Pressing the Tab key will automatically complete the command:

1
[root@lht example]# ls ls-output.txt

Note that autocompletion requires a unique match in the directory.

If you input the following:

1
[root@lht example]# ls D

Pressing the Tab key will not complete the command because multiple matches exist, so you need to type more. You can also press the Tab key twice to display all current matching options:

1
2
[root@lht example]# ls D
Desktop Documents

Then you can further refine your input:

1
[root@lht example]# ls Do

Pressing the Tab key again will complete the command.

Autocompletion also works for variables (if the word starts with a “$”), usernames (if the word starts with “~”), commands (if the word is the first on a line), and hostnames (if the word starts with “@”). Hostname autocompletion only applies to hostnames included in the /etc/hosts file.

Some related shortcuts:

Key Action
Alt-? Show the list of possible completions. In most systems, you can also do this by pressing the Tab key twice.
Alt-* Insert all possible completions.

Utilizing Command History

Searching History

To view your command history, you can use:

1
[root@lht example]# history | less

Bash saves the last 500 commands by default, though most distributions now set this value to 1000. We’ll discuss how to adjust this later.

If you want to find commands related to the /bin directory, you can do the following:

1
[root@lht example]# history | grep /bin/

Suppose you get an output like this:

1
727  ll /bin/

Here, 727 is the command number in the history, which you can use to re-execute the command using history expansion.

1
2
3
4
5
6
7
8
[root@lht example]# !727
ll /bin/
total 577696
-rwxr-xr-x. 1 root root 41544 Oct 31 2018 [
-rwxr-xr-x 1 root root 107904 Jan 22 2019 a2p
-rwxr-xr-x 1 root root 1661 Nov 21 2015 abs2rel
-rwxr-xr-x. 1 root root 29200 Oct 30 2018 addr2line
-rwxr-xr-x. 1 root root 29 Oct 31 2018 alias

bash also supports incremental searching of the command history. By pressing Ctrl-R, you can start searching, and as you type, bash will display results that match your input. The more you type, the more precise the search results become.

If you find the desired result, you can press Enter to execute that command, or if you want to edit the command before running it, you can press Ctrl-L to copy the result into the current command line.

To see the next matching result, you can press Ctrl-R again, and you can exit the search by pressing Ctrl-G or Ctrl-C.

When you press Ctrl-R, the prompt changes to:

1
(reverse-i-search)`':

You can then type the text you want to search for:

1
(reverse-i-search)`bin': file $(ls /bin/* | grep zip)

Pressing Ctrl-R again allows you to view the previous match:

1
(reverse-i-search)`bin': ll /bin/ | grep zip

Pressing Ctrl-J will copy the command to the command line:

1
[root@lht ~]# ll /bin/ | grep zip

After editing the command, you can execute it:

1
2
3
4
5
[root@lht ~]# ll /bin/ | grep zip > ls-output.txt 
[root@lht ~]# cat ls-output.txt
-rwxr-xr-x. 1 root root 3307 Jul 13 2018 gpg-zip
-rwxr-xr-x. 1 root root 2253 Apr 11 2018 gunzip
-rwxr-xr-x. 1 root root 100800 Apr 11 2018 gzip

Here are some related key combinations:

Key Action
Ctrl-p Move to the previous history entry. Similar to the up arrow key.
Ctrl-n Move to the next history entry. Similar to the down arrow key.
Alt-< Move to the beginning of the history list.
Alt-> Move to the end of the history list, i.e., the current command line.
Ctrl-r Reverse incremental search. Searches upward from the current command line.
Alt-p Reverse search, non-incremental. (Input the string to search, press Enter to execute the search.)
Alt-n Forward search, non-incremental.
Ctrl-o Execute the current item from the history list and move to the next. Useful for executing a series of commands from the history list.

History Command Expansion

Sequence Action
!! Repeat the last executed command. It might be easier to press the up arrow key and the enter key.
! number Repeat the command from line number in the history list.
! string Repeat the command from the recent history list that starts with this string.
! ? string Repeat the command from the recent history list that contains this string.

Be cautious when using “! string” and “!? string” formats, unless you are completely sure of the contents of the history list entries.

  • Title: Linux command line 6 - Advanced Keyboard Tricks
  • Author: LHT
  • Created at : 2023-01-06 08:00:00
  • Link: https://blog.327774.xyz/2023/01/06/linux/Linux command line 6 - Advanced Keyboard Tricks/
  • License: This work is licensed under CC BY-NC-SA 4.0.