Frequently Used Linux Shortcuts (Part 1)

In the Linux environment, many shortcuts are used regularly. Here, I share some shortcuts and methods that can improve efficiency, hoping they will be useful to you.

In Linux, implementing command auto-matching from history mainly relies on command line completion and history command search. Below are several commonly used methods:

1. Quickly match historical commands using arrow keys

This is the most basic and commonly used method:

  • Type part of a command in the terminal (for example, type <span>cd</span>), then press up arrow ↑ or down arrow ↓. The terminal will automatically match commands from the history that start with that prefix, switching one at a time until the target command is found.

2. Use <span>Ctrl + R</span> to search historical commands (recommended)

This is a more efficient way to search historical commands, supporting fuzzy matching:

  • Press <span>Ctrl + R</span> in the terminal to enter reverse search mode (the terminal will display <span>(reverse-i-search)</span> prompt).
  • Type part of a keyword from the command you remember (for example, type <span>ssh</span>), and the terminal will automatically match commands from history that contain that keyword, prioritizing the most recent records.
  • If the match is not the target, continue pressing <span>Ctrl + R</span> to switch to the previous match. Once found, press Enter to execute directly, or press right arrow → to edit before executing.

3. Configure <span>bash</span> auto-completion (enhanced matching)

<span>bash</span> supports basic completion by default (press <span>Tab</span> to complete commands / filenames), and you can configure it to enhance historical command completion:

  • Edit the <span>~/.bashrc</span> file (the configuration file for <span>bash</span>):
vim ~/.bashrc
  • Add the following configuration (enable historical command completion, press <span>Tab</span> to match history):
# Enable historical command completion
bind '"\e[A": history-search-backward'   # Up arrow matches prefix
bind '"\e[B": history-search-forward'    # Down arrow matches prefix
  • After saving, apply the configuration:
source ~/.bashrc
  • Effect: When typing a command prefix (for example, <span>git</span>), pressing up / down arrow will only match historical commands that start with that prefix (more precise than the default full history match).

In the Linux terminal (or command line interface), moving the cursor mainly relies on keyboard shortcuts. The shortcuts vary slightly in different scenarios (such as normal input, editing commands, specific tools). Here are the most commonly used general cursor movement shortcuts:

1. Basic cursor movement (applicable to most terminals and shells, such as bash, zsh)

Shortcut Description
<span>left arrow ←</span> Move the cursor left by one character
<span>right arrow →</span> Move the cursor right by one character
<span>up arrow ↑</span> Switch to the previous historical command
<span>down arrow ↓</span> Switch to the next historical command
<span>Ctrl + A</span> Quickly move the cursor to the beginning of the command line (start)
<span>Ctrl + E</span> Quickly move the cursor to the end of the command line (end)
<span>Alt + B</span> Move the cursor left by one word (separated by spaces / symbols)
<span>Alt + F</span> Move the cursor right by one word

2. Combined deletion / editing movement (commonly used)

Shortcut Description
<span>Ctrl + U</span> Delete all content from the cursor to the beginning
<span>Ctrl + K</span> Delete all content from the cursor to the end
<span>Ctrl + W</span> Delete one word to the left of the cursor

3. Cursor movement in editors like <span>vim</span> (additional notes)

If you use <span>vim</span> to edit files in the terminal, there are more efficient shortcuts for moving the cursor (no need for arrow keys)

  • <span>h: move left by one character</span>
  • <span>j: move down one line</span>
  • <span>k: move up one line</span>
  • <span>l: move right by one character</span>
  • <span>w: move right by one word</span>
  • <span>b: move left by one word</span>
  • <span>0 (zero): move to the beginning of the line</span>
  • <span>$: move to the end of the line</span>
  • <span>gg: move to the beginning of the file</span>
  • <span>G: move to the end of the file</span>

Summary

  • During normal command line input, <span>Ctrl + A</span> (to the beginning), <span>Ctrl + E</span> (to the end), <span>Alt + B/F</span> (move by word) are frequently used efficient shortcuts, saving more time than moving one by one with arrow keys.
  • Remembering these shortcuts can significantly improve command line operation efficiency, especially when dealing with long commands.

Leave a Comment