Common Tools for the Linux Command Line

In the Linux command line, there are numerous shortcuts (often referred to as keyboard bindings or hotkeys) that can significantly enhance efficiency. These shortcuts are primarily provided by Bash (or other shells like Zsh) and the underlying readline library. Below are some of the most commonly used and useful command line shortcuts categorized:

1. In-line Editing

These shortcuts are used to modify the current command being input.

Ctrl + A: Move the cursor to the beginning of the current line.
Ctrl + E: Move the cursor to the end of the current line.
Ctrl + B: Move the cursor left by one character (Backward).
Ctrl + F: Move the cursor right by one character (Forward).
Alt + B: Move the cursor left by one word.
Alt + F: Move the cursor right by one word.
Ctrl + W: Delete all characters from the cursor position to the beginning of the previous word (cut a word).
Alt + D: Delete all characters from the cursor position to the end of the current word.
Ctrl + K: Delete all characters from the cursor position to the end of the current line.
Ctrl + U: Delete all characters from the cursor position to the beginning of the current line.
Ctrl + Y: Paste the last deleted content (from Ctrl+W, Ctrl+K, Alt+D, Ctrl+U, etc.). This is similar to a "clipboard."
Ctrl + D: Delete the character at the cursor position. If the line is empty, it logs out of the current shell session.
Backspace / Ctrl + H: Delete the character before the cursor.
Ctrl + T: Swap the character before the cursor with the character at the cursor position.
Alt + T: Swap the word at the cursor with the previous word.
Alt + U: Convert the characters from the cursor position to the end of the current word to uppercase.
Alt + L: Convert the characters from the cursor position to the end of the current word to lowercase.
Alt + C: Convert the first character from the cursor position to the end of the current word to uppercase and the rest to lowercase.
Ctrl + _ or Ctrl + X Ctrl + U: Undo the last editing action.

2. History Commands

Used to access and manage previously executed commands.

Up Arrow (↑): Show the previous history command.
Down Arrow (↓): Show the next history command.
Ctrl + R: Enter history command search mode. Type a keyword, and it will display the most recent command containing that keyword. Repeatedly pressing Ctrl + R can find earlier matches.
Ctrl + P: Same as Up Arrow.
Ctrl + N: Same as Down Arrow.
Alt + <: Jump to the first command in the history list.
Alt + >: Jump to the last command in the history list.
Alt + . or Alt + _: Insert the last parameter of the previous command. Repeatedly pressing can cycle through the last parameters of previous commands.
!!: Run the previous command.
!$: Insert the last parameter of the previous command (same effect as Alt + .).
!*: Insert all parameters of the previous command.
!<string>: Run the most recent command starting with <string>.
!?<string>?: Run the most recent command containing <string>.
history: Display the complete history command list.

3. Process Control

Used to manage currently running commands or programs.

Ctrl + C: Terminate the currently running program (send SIGINT signal).
Ctrl + Z: Suspend the currently running program and place it in the background (send SIGSTOP signal). It can be resumed using the fg (foreground) or bg (background) commands.
Ctrl + D: If the cursor is at the beginning of an empty line, it logs out of the current shell. Otherwise, it indicates the end of file (EOF).

4. Terminal Control

Used to control the display or behavior of the terminal.

Ctrl + L: Clear the terminal screen, but keep the current command line unchanged.
Ctrl + S: Pause terminal output (XOFF). This is useful when running a command that outputs a lot of text but you don't want it to scroll too fast. Note: Some terminals or configurations may map Ctrl + S to save the session or disable it. If it doesn't work, try the stty -ixon command to enable it.
Ctrl + Q: Resume terminal output (XON), releasing the pause from Ctrl + S.
Ctrl + V: Insert the literal value of the next key. For example, if you want to input the Ctrl + C character itself in the command line instead of terminating the program, you can press Ctrl + V and then Ctrl + C.

5. File and Directory Completion

Tab: Pressing Tab once: auto-completes the current input of file names, directory names, command names, or variable names. Pressing Tab twice: if there are multiple matches, it will list all possible completion options.

6. Other Common Shortcuts

Home: Same as Ctrl + A, moves the cursor to the beginning of the line.
End: Same as Ctrl + E, moves the cursor to the end of the line.
Page Up / Page Down: In some terminals, used to scroll up and down the terminal's history buffer.
Esc: In some terminals, the Esc key can be used as a prefix for the Alt key. For example, Esc followed by f is equivalent to Alt + F.
Ctrl + X, Ctrl + E: Open the current command line in the default editor (usually vi or emacs). After editing and saving, the command will be executed automatically. This is very useful for long or complex commands.

Tip: Alternative for the Alt key: If your terminal or keyboard does not have an Alt key, you can usually use the Esc key as a prefix. For example, pressing Esc followed by F has the same effect as Alt + F. Custom Shortcuts: You can customize readline library shortcuts by modifying the ~/.inputrc file. For example, you can bind a specific key combination to a particular command or function. Zsh Enhancements: If you are using Zsh, it has more powerful auto-completion features and richer history command handling capabilities, such as Ctrl+X h to display the history command list and select. Practice: Mastering these shortcuts requires practice. Try to deliberately apply them in your daily usage, and you will soon find a significant improvement in command line efficiency.

Leave a Comment