Bash Shortcuts Cheat Sheet
Category Programming Techniques
In Linux, Bash is indispensable. Most of the management and operations in Linux are performed in Bash. Therefore, to improve work efficiency under Linux, the question naturally becomes how to use Bash efficiently. Using some shortcuts in Bash is the simplest and most direct way to enhance efficiency. This article is aimed at achieving this goal!
Some explanations about shortcuts:
- CTRL=C: This key refers to the Ctrl key on the PC keyboard
- ALT=M: This key is the ALT key on the PC keyboard. If your keyboard does not have this key, you can try using the ESC key as a substitute
- SHIFT=S: This key is the Shift key on the PC
- ESC=E: This key is the ESC key on the PC keyboard, usually located at the top left of the keyboard
- BACKSPACE=DEL: This key is the Backspace key on the PC keyboard, typically located at the top right of the main keyboard area
- In the text, content enclosed in "[]" represents the shortcut, and content on both sides of "-" means holding down the left key and then pressing the right key. Content on both sides of "," means pressing the left key first, releasing it, and then pressing the right key. For example, [CTRL-v] means pressing the Ctrl key and, without releasing it, pressing the v key.
- By default, the composition format of shortcuts is: <CTRL | ALT | ESC >-[SHIFT-]<char>. That is, starting with one of Ctrl, Alt, or Esc, followed by a hyphen, Shift, hyphen, and a character. The Shift in brackets and "-" can sometimes be omitted.
- By default, only the last key in the shortcut is a character, and the other keys are function keys
- Shortcuts like [CTRL-?] appear, where "?" is a character that requires the Shift key to obtain, so such shortcuts default to using [CTRL-SHIFT-?]
Note that in Bash, shortcuts may be written in octal or hexadecimal form (following the escape character); shortcuts in script files are not always effective. Also, there is a rule: shortcuts starting with Ctrl are generally for characters, while those starting with Alt are generally for words.
In Bash, if using a shell script file, shortcuts may not be the same; sometimes, the same shortcut may have different behaviors. This is usually due to different modes of Bash. You can adjust the mode using the set command:
set -o emacs
## Switch to emacs mode
set -o vi
## Switch to vi mode
set -o
## View the current option settings
This is the option setting for Bash, which you can set according to the specific situation. This article uses the emacs mode.
Shortcut | Description |
---|---|
CTRL-A | Move the cursor to the beginning of the line (in the command line) |
CTRL-B | Backspace (non-destructive), this only moves the cursor position back by one position. |
CTRL-C | Interrupt, terminate a foreground job. |
CTRL-D | "EOF" (end of file). It is used to indicate the end of standard input (stdin). When typing text in a console or xterm window, CTRL-D deletes the character under the cursor. Exiting from a shell (similar to exit). If no characters are present, CTRL-D logs out the session. In an xterm window, it may close the window. |
CTRL-E | Move the cursor to the end of the line (in the command line) |
CTRL-F | Move the cursor forward by one character (in the command line) |
CTRL-G | BEL. On some old printer terminals, this would trigger a bell. In an xterm terminal, it might produce a beep. |
CTRL-H | Rubout (destructive backspace). When moving the cursor back, it also erases the character in front of the cursor. |
CTRL-I | Horizontal tab. |
CTRL-J | New line (line feed and move to the beginning of the line). In scripts, it may also be represented as an octal form (‘/012′) or hexadecimal form (‘/x0a’). |
CTRL-K | Vertical tab. When typing text in a console or xterm window, CTRL-K deletes all characters from the cursor position to the end of the line. In scripts, it may also be represented as an octal form (‘/013′) or hexadecimal form (‘/x0b’). In scripts, CTRL-K may behave differently. |
CTRL-L | Formfeed, clear the screen. Clears the terminal screen. In a terminal, this command acts the same as the clear command. When sent to a printer, Ctrl-L skips directly to the end of the paper sheet. |
CTRL-M | Carriage return. |
CTRL-N | Erase a line of text recalled from the history buffer (in the command line). If the current input is selected from the history, each press moves to an earlier command. |
CTRL-O | Produce a new line (in the command line). |
CTRL-P | Recall the previous command from the history buffer (in the command line). This shortcut recalls commands in order from most recent to least recent. |
CTRL-Q | Resume (XON). Restore/unfreeze, this command restores the terminal's stdin, see CTRL-S. |
CTRL-R | Backwards search in the history buffer for text (in the command line). Note: After pressing, the prompt will change to (reverse-i-search)’: the input search content appears inside single quotes, and the colon is followed by the most recent and matching historical command. |
CTRL-S | Suspend (XOFF), freeze. This freezes the terminal's stdin. To restore, press CTRL-Q. |
CTRL-T | Swap the character at the cursor position with the character at the previous cursor position (in the command line). |
CTRL-U | Erase all characters from the cursor position to the beginning of the line. In some settings, CTRL-U may erase the entire line input. |
CTRL-V | When entering text, pressing C-V allows inserting control characters. For example, echo -e ‘/x0a’; and echo <CTRL-V><CTRL-J>; have the same effect. This function is very effective in text editors. |
CTRL-W | When typing text in a console or an xterm window, CTRL-W deletes the content between the cursor and the first blank character to the left. In some settings, CTRL-W deletes the content from the cursor to the first non-alphanumeric character to the left. |
CTRL-X | In some text processing programs, this control character cuts the highlighted text and copies it to the clipboard. |
CTRL-Y | Paste back previously cleared text (mainly for CTRL-U or CTRL-W). |
CTRL-Z | Pause a foreground job; also used as a substitution operation in some text processing programs; as the EOF (End-of-file) character in MSDOS file systems. |
CTRL-/ | Exit. Similar to CTRL-C, it may also dump a "core" file to your working directory (this file may be useless to you). |
CTRL-/ | Undo operation. |
CTRL-_ | Undo operation. |
CTRL-xx | Toggle between the beginning of the line and the cursor position, where "xx" represents two "x" characters. |
ALT-B | Move the cursor back by one word, where words are delimited by non-letters (moves to the beginning of the current word). |
ALT-F | Move the cursor forward by one word (moves to the end of the current word). |
ALT-D | Delete all content from the cursor position to the end of the word (or the entire word if the cursor is at the beginning). |
ALT-BASKSPACE | Delete all content from the cursor position to the beginning of the word. |
ALT-C | Capitalize the letter at the cursor position (or the first letter of the word if the cursor is at the beginning or before). |
ALT-U | Convert all letters from the cursor position to the end of the word to uppercase. |
ALT-L | Convert all letters from the cursor position to the end of the word to lowercase. |
ALT-R | Cancel all changes and restore the current line to its original state in the history record (if the current command is from the history, if manually entered, it will clear the line). |
ALT-T | When words are present on both sides of the cursor, swap the positions of the words. For example: abc <ALT-T>bcd -> bcd abc |
ALT-. | Use the last word of the previous command (the command itself is also a word, see the concept of word indicators in the Bang command in the next article). |
ALT-_ | Same as ALT-. |
ALT-numeric | This numeric value can be positive or negative. This key alone has no effect and must be followed by other content. If followed by a character, it indicates the number of repetitions. For example: [ALT-10,k] will insert 10 k characters at the cursor position (negative values are invalid in this case); if followed by a command, the number affects the execution result of the subsequent command, such as: [ALT--10,CTRL-D] will perform the operation 10 times in the opposite direction (negative number) to the default direction of CTRL-D. |
ALT-< | Move to the first command in the history record. |
ALT-> | Move to the last command in the history, which is the current line being entered (empty if no input). |
ALT-P | Start searching forward from the current line, moving "up" if necessary, using non-incremental search to find the provided string. |
ALT-N | Start searching backward from the current line, moving "down" if necessary, using non-incremental search to find the provided string. |
ALT-CTRL-Y | Insert the first argument of the previous command (usually the second word of the previous line) at the mark. If an argument n is provided, insert the nth word of the previous command (words in the previous line are numbered starting from 0, see history expansion). Negative arguments insert the nth word starting from the end of the previous command. The argument n is passed via M-No., such as: [ALT-0,ALT-CTRL-Y] inserts the 0th word (the command itself) of the previous command. |
ALT-Y | Poll the deletion ring and copy the new top text. This command can only be used after yank [CTRL-Y] or yank-pop [M-Y]. |
ALT-? | List the entries that can complete before the mark. |
ALT-* | Insert all text entries that the [ALT-?] command can generate before the mark. |
ALT-/ | Attempt filename completion for the text before the mark. [CTRL-X,/] treats the text before the mark as a filename and lists the possible completions. |
ALT-~ | Treat the text before the mark as a username and attempt completion. [CTRL-X,~] lists the possible completions for the username before the mark. |
ALT-$ | Treat the text before the mark as a Shell variable and attempt completion. [CTRL-X,$] lists the possible completions for the variable before the mark. |
ALT-@ | Treat the text before the mark as a hostname and attempt completion. [CTRL-X,@] lists the possible completions for the host before the mark. |
ALT-! | Treat the text before the mark as a command name and attempt completion. Command name completion will use aliases, reserved words, Shell functions, shell internal commands, and finally executable file names in order. [CTRL-X,!] treats the text before the mark as a command name and lists the possible completions. |
ALT-TAB | Compare the text before the mark with the text in the history record to find a match and attempt completion. |
ALT-{ | Perform filename completion, listing the possible completions within braces so that the shell can use them. |
In Bash, if you can use shortcuts properly, the operation of the Linux system will become very fast. For example, when we use cat to create a file, we can use the shortcut [CTRL-D]:
## Without shortcuts
cat >>/tmp/test<<_EOF
## Here is the content
## Finally, we need to input _EOF on a new line
## The content will only be written to the file when _EOF is seen by cat
## Use shortcut keys
cat >>/tmp/test
## Enter content here
## After finishing input, end with [CTRL-D]
Sometimes we need to create a file and then operate on it:
touch /tmp/a-test-file-from-blog.useasp.net
## Without shortcut keys, the file name needs to be re-entered
chmod u+x /tmp/a-test-file-from-blog.useasp.net
## Use shortcut keys
chmod u+x <ALT-.>
## Shortcut key [M-.] automatically appends the last argument above
How about that, is it more efficient?
Of course, Bash shortcuts can only become truly efficient through continuous use. At the beginning, when you have to think for a long time about which shortcut key to use, efficiency is hard to achieve—but sharpening the axe does not delay the chopping of wood, the initial investment is worthwhile.
If you want your Bash to be a bit different, you can also customize shortcut keys using the bind command. Bash shortcuts are actually provided by Readline, so the shortcut key settings here are essentially configuring Readline. There are two types of shortcuts in Readline: one is for internal functions of Readline, and the other is for executing Shell commands. The settings are slightly different:
## View the function names available in Readline
bind -l
## View the currently bound key configurations and their corresponding functions
bind -v
## View already bound shortcuts
bind -p
## Bind custom shortcut to execute shell command
bind -x '"/C-x/C-l":ls -al'
## After binding, pressing [C-x,C-L] will execute ls -al
## Bind to internal function
bind "/C-x":backward-delete-char
## This is the function backward-delete-char from the Readline library
This setup is only valid for the current session. Once the session is lost, the shortcut keys set up this way will be lost. To make the shortcut keys permanently effective, we need to write the shortcut key configuration into a file. In Linux systems, there are two places where shortcut keys can be permanently saved: global and user configuration files. The global one is /etc/inputrc, and the user one is in the user's home directory ~/.inputrc. The global one affects all users, while the one in the user's home directory only affects the corresponding user. The inputrc file looks something like this:
## Example from the default configuration file of CentOS 6.4
$if mode=emacs
# for linux console and RH/Debian xterm
"/e[1~": beginning-of-line
"/e[4~": end-of-line
# commented out keymappings for pgup/pgdown to reach begin/end of history
#"/e[5~": beginning-of-history
#"/e[6~": end-of-history
"/e[5~": history-search-backward
"/e[6~": history-search-forward
"/e[3~": delete-char
"/e[2~": quoted-insert
"/e[5C": forward-word
"/e[5D": backward-word
"/e[1;5C": forward-word
"/e[1;5D": backward-word
# for rxvt
"/e[8~": end-of-line
"/eOc": forward-word
"/eOd": backward-word
# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
"/eOH": beginning-of-line
"/eOF": end-of-line
# for freebsd console
"/e[H": beginning-of-line
"/e[F": end-of-line
$endif
Notes:
In the configuration file, /C represents CTRL, /M represents ALT, /e represents ESC, // is a backslash \, /' is a single quote ', /" is a double quote ";
/C- control prefix /M- meta prefix /e an escape character // backslash /" literal ", a double quote /’ literal ’, a single quote
To view the character sequence of a function key, you can use [CTRL-V] or enter cat and press Enter, then directly press the shortcut key;
The configuration file may use octal or hexadecimal to represent characters.
If we set appropriate shortcut keys for common operations, commands that used to take a long time to type might be handled with a single shortcut key, which will undoubtedly greatly improve our work efficiency! Use shortcuts, young man!
Source: http://www.codeceo.com/article/bash-shortcut.html
#
-
** Huacai
* lih**[email protected]
Expect ctrl + u to delete from the cursor to the beginning of the line, rather than deleting the entire line. Try this setting (this is zsh):
echo 'bindkey \^U backward-kill-line' >> ~/.zshrc
source ~/.zshrc
** Huacai
* lih**[email protected]
** Click me to share notes
-
-
-