directory

 

Introduction to the

The cursor

Clear the screen

Edit operation

Automatic completion

Operating history

Basic usage

History command

Related shortcut keys

Other shortcut keys


Introduction to the

Bash comes with the Readline library, which provides many of the “line action” features, such as auto-completion of commands, that can greatly speed up operations.

The library uses Emacs shortcuts by default, or can be changed to Vi shortcuts.

$ set -o vi
Copy the code

The following command can be changed back to the Emacs shortcut key.

$ set -o emacs
Copy the code

If you want to permanently change edit mode (Emacs/Vi), you can write the command in the ~/.inputrc file, which is the Readline configuration file.

set editing-mode vi
Copy the code

All the shortcut keys described in this chapter belong to Emacs mode. Vi mode shortcut keys, readers can refer to the Vi editor tutorial.

Bash turns this library on by default, but allows it to be turned off.

$ bash --noediting
Copy the code

In the command above, the –noediting parameter closes the Readline library and starts Bash without line operations.

 

The cursor

Readline provides shortcuts for moving the cursor quickly.

  • Ctrl + a: moves to the top of the line.
  • Ctrl + b: Moves one character to the beginning of the line, acting as the left arrow.
  • Ctrl + e: moves to the end of the line.
  • Ctrl + f: Moves one character to the end of the line, as does the right arrow.
  • Alt + f: Moves to the end of the current word.
  • Alt + b: Moves to the beginning of the current word.

Alt, the shortcut key above, can also be replaced by ESC.

 

Clear the screen

The shortcut key Ctrl + L clears the screen, that is, moves the current line to the first line on the screen. This command has the same effect as the clear command.

 

Edit operation

The following shortcut keys can edit the contents of the command line.

  • Ctrl + d: Deletes the character at the cursor position (delete).
  • Ctrl + w: Deletes the word before the cursor.
  • Ctrl + tTranspose the character at the cursor position with the character one bit before it.
  • Alt + tTranspose the word at the cursor position with the word one bit before it.
  • Alt + l: Change the cursor position to lowercase (lowercase) at the end of the word.
  • Alt + u: Moves the cursor position to uppercase at the end of the word.

Using Ctrl + D will cause the current Shell to exit if there are no characters on the current line, so be careful.

Cut and paste the shortcut keys below.

  • Ctrl + k: Cuts the text with the cursor position to the end of the line.
  • Ctrl + u: Cuts the text with the cursor position to the top of the line.
  • Alt + d: Clips the cursor position to the end of the word.
  • Alt + Backspace: Cuts the text with the cursor position to the word head.
  • Ctrl + y: Pastes text at the cursor position.

Similarly, the Alt key can be replaced with the Esc key.

 

Automatic completion

When you are halfway through a command, press Tab. Readline automatically completes the command or path. For example, if you type cle and press Tab, Bash automatically completes this command to clear.

If there are multiple qualified commands or paths, you need to press the Tab key twice, and Bash prompts you for all qualified commands or paths.

In addition to commands or paths, Tab can complete other values. If a value starts with $, pressing Tab completes the variable; If the user name starts with ~, complete the user name. If it starts with @, complete the hostname (hostname). The hostname is the one listed in the /etc/hosts file.

The shortcut keys related to auto completion are as follows.

  • Tab: automatic completion is complete.
  • Alt + ?: Lists possible completions. Same as pressing the Tab key twice.
  • Alt + /: Attempts file path completion.
  • Ctrl + x /: according to the firstCtrl + xAnd then according to the/Is the same asAlt + ?To list possible file path completions.
  • Alt + !: Command completion.
  • Ctrl + x !: according to the firstCtrl + xAnd then according to the!Is the same asAlt + !Command completion.
  • Alt + ~: User name completion.
  • Ctrl + x ~: according to the firstCtrl + xAnd then according to the~Is the same asAlt + ~, user name completion.
  • Alt + $: Variable name completion.
  • Ctrl + x $: according to the firstCtrl + xAnd then according to the$Is the same asAlt + $, variable name completion.
  • Alt + @: Host name completion.
  • Ctrl + x @: according to the firstCtrl + xAnd then according to the@Is the same asAlt + @, host name completion.
  • Alt + *: Inserts all possible completions at once on the command line.
  • Alt + Tab: try to use.bash_historyInside the previous command to complete.

The Alt key above can also be replaced with the ESC key.

Operating history

Basic usage

Bash keeps a history of the user’s actions, that is, every command entered by the user is recorded. When exiting the current Shell, Bash writes the history of the user’s operations in the current Shell to the ~/.bash_history file, which stores 500 operations by default.

The environment variable HISTFILE always points to this file.

$ echo $HISTFILE
/home/me/.bash_history
Copy the code

After you have the operation history, you can use arrow keys ↑ and ↓ to quickly view the previous and next commands.

The following method can quickly execute previously executed commands.

$ echo Hello World
Hello World

$ echoGoodbye Goodbye $ ! eecho Goodbye
Goodbye
Copy the code

In the example above,! E indicates to find the latest command starting with e in the operation history and execute it. Bash prints the command echo Goodbye and executes it directly.

Similarly,! Echo also executes the last command that starts with echo.

$!echo
echo Goodbye
Goodbye

$ !echo H
echo Goodbye H
Goodbye H

$ !echo H G
echo Goodbye H G
Goodbye H G
Copy the code

Note that! The string syntax only matches commands, not arguments. So! Instead of echo Hello World, echo H executes echo Goodbye, appending the H argument to the command. Similarly,! Echo H G is also equivalent to echo Goodbye followed by H G.

Finally, pressing Ctrl + R shows a history of the action, which you can move up and down using the arrow keys to select the command to execute. You can also type the first letter of a command, and the Shell will automatically query and display matching results in the history file.

 

History command

The history command displays the history of the operation, that is, the contents of the.bash_history file.

$ history. 498echo Goodbye
499 ls ~
500 cd
Copy the code

The advantage of using this command, rather than reading the.bash_history file directly, is that it precedes all operations with line numbers, with the most recent operations coming last and having the largest line number.

By customizing the environment variable HISTTIMEFORMAT, you can display the time of each operation.

$ export HISTTIMEFORMAT='%F %T '
$ history
1  2013-06-09 10:40:12   cat /etc/issue
2  2013-06-09 10:40:12   clear
Copy the code

In the code above, %F equals %Y – %m – %d and %T equals %H: %m: %S.

As long as the HISTTIMEFORMAT environment variable is set, the execution time stamp of the command is saved in the.bash_history file. If not set, the timestamp is not saved.

If you do not want to save the history of this operation, set the environment variable HISTSIZE to 0.

export HISTSIZE=0
Copy the code

If HISTSIZE=0 writes to the ~/.bashrc file in the user’s home directory, the user’s operation history is not preserved. If /etc/profile is written, the entire system does not retain the history of the operation.

To search for a command previously executed, run the grep command to search for the operation history.

$ history | grep /usr/bin
Copy the code

The command above returns the commands that contain /usr/bin in the.bash_history file.

Each entry in the history of the operation is numbered. After you know the command number, you can use an exclamation mark (!) + number to execute the command. If you wanted to execute command 8 in.bash_history, you could do something like this.

$ !8
Copy the code

The -c parameter of the history command clears the history of the operation.

$ history -c
Copy the code

 

Related shortcut keys

Here are some shortcuts related to action history.

  • Ctrl + p: Displays the previous command with the same effect as the previous arrow.
  • Ctrl + n: Displays the next command, same as the down arrow (next).
  • Alt + <: Displays the first command.
  • Alt + >: Displays the last command, which is the current command.
  • Ctrl + o: Executes the current entry in the history file and automatically displays the next command. This is useful for repeating a sequence of commands.

Exclamation point! The shortcut keys are as follows.

  • !!!!!: Executes the last command.
  • ! nThe line number in the execution history file isnThe command.
  • ! -n: Before the current command is executednBar command.
  • ! string: Executes the most recent string to specifystringInitial command.
  • ! ? string: Executes the last include stringstringThe command.
  • ^string1^string2: Performs the latest includestring1To replace it withstring2.

 

Other shortcut keys

  • Ctrl + j: is equivalent to LINEFEED.
  • Ctrl + m: Equivalent to CARRIAGE RETURN.
  • Ctrl + o: is equivalent to the enter key and displays the history of the next command.
  • Ctrl + v: Converts the next input special character to a literal, such as carriage return^M.
  • Ctrl + [: Is equivalent to ESC.
  • Alt + .: Inserts the last word of the previous command.
  • Alt + _: equivalent toAlt + ..

The Alt +. Shortcut above can be handy for very long file paths. Because the last argument to a Unix command is usually the file path.

$ mkdir foo_bar
$ cd # Press Alt +.
Copy the code

In the example above, pressing Alt +. After the CD command inserts foo_bar automatically.

 

The next section of the Bash script tutorial directory stack