Bash line operation

Introduction

Bash has a built-in Readline library, which has many "line operations" functions provided by this library, such as automatic command completion, which can greatly speed up operations.

This library uses Emacs shortcut keys by default, but it can also be changed to Vi shortcut keys.

$ set -o vi

The following command can be changed back to Emacs shortcut keys.

$ set -o emacs

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

set editing-mode vi

The shortcut keys introduced in this chapter belong to Emacs mode. For the shortcut keys of Vi mode, readers can refer to the tutorial of the Vi editor.

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

$ bash --noediting

In the above command, the --noediting parameter closes the Readline library, and Bash is started without the line operation function.

Cursor movement

Readline provides shortcut keys to quickly move the cursor.

  • Ctrl + a: Move to the beginning of the line.
  • Ctrl + b: Move one character to the beginning of the line, which has the same effect as the left arrow.
  • Ctrl + e: Move to the end of the line.
  • Ctrl + f: Move one character to the end of the line, which has the same effect as the right arrow.
  • Alt + f: Move to the end of the current word.
  • Alt + b: Move to the beginning of the current word.

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

Clear screen

The shortcut key Ctrl + l can clear the screen, that is, move the current line to the first line of the screen, which has the same effect as the clear command.

Edit operation

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

  • Ctrl + d: Delete the character at the cursor position (delete).
  • Ctrl + w: Delete the word in front of the cursor.
  • Ctrl + t: Transpose the character at the cursor position with the character preceding it (transpose).
  • Alt + t: The word at the cursor position is transpose with the word preceding it (transpose).
  • Alt + l: Convert the cursor position to the end of the word to lowercase (lowercase).
  • Alt + u: Convert the cursor position to the end of the word to uppercase (uppercase).

When using Ctrl + d, if the current line does not contain any characters, it will cause the current Shell to exit, so be careful.

Cut and paste shortcut keys are as follows.

  • 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.
  • Ctrl + y: Paste text at the cursor position.

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

Automatic completion

When the command is halfway through, you can press the Tab key, and Readline will automatically complete the command or path. For example, if you type cle and press the Tab key, Bash will automatically complete this command to clear.

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

In addition to commands or paths, Tab can also complete other values. If a value begins with $, press Tab to complete the variable; if it begins with ~, then complete the user name; if it begins with @, complete the hostname, hostname The host listed in the /etc/hosts file shall prevail.

The shortcut keys related to auto-completion are as follows.

-Tab: Complete automatic completion.

  • Alt + ?: List possible completions, which has the same effect as pressing the Tab key twice.
  • Alt + /: Try file path completion.
  • Ctrl + x /: Press Ctrl + x first, then /, which is equivalent to Alt + ?, and list possible file path completions.
  • Alt + !: Command completion.
  • Ctrl + x !: Press Ctrl + x first, then press !, which is equivalent to Alt + !, command completion.
  • Alt + ~: User name completion.
  • Ctrl + x ~: Press Ctrl + x first, and then ~, which is equivalent to Alt + ~, user name completion.
  • Alt + $: Variable name completion.
  • Ctrl + x $: Press Ctrl + x first, then press $, which is equivalent to Alt + $, variable name completion.
  • Alt + @: host name completion.
  • Ctrl + x @: Press Ctrl + x first, and then @, which is equivalent to Alt + @, host name completion.
  • Alt + *: Insert all possible completions in the command line at once.
  • Alt + Tab: Try to use the previously executed command in .bash_history to complete it.

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

Operation history

Basic usage

Bash will keep the user's operation history, that is, every command entered by the user will be recorded. Once you have the operation history, you can use the arrow keys and to quickly browse the previous and next commands.

When exiting the current Shell, Bash will write the user's operation history in the current Shell into 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

The history command will output the entire contents of this file. The user can see all the commands that have been executed recently, and each command has a line number before it. The closer the command is, the lower it is.

$ history
...
498 echo Goodbye
499 ls ~
500 cd

When entering a command, press the shortcut key Ctrl + r to search the operation history and select the previously executed command. At this time, when you type the beginning of the command, Shell will automatically query and display the most recent matching result in the history file. At this time, press the Enter key to execute that command.

The following method can quickly execute previously executed commands.

$ echo Hello World
Hello World

$ echo Goodbye
Goodbye

$ !e
echo Goodbye
Goodbye

In the above example, !e means to find and execute the most recent command starting with e in the operation history. Bash will first output the command echo Goodbye, and then execute it directly.

Similarly, !echo will also execute the most recent command beginning with echo.

$! echo
echo Goodbye
Goodbye

$! echo H
echo Goodbye H
Goodbye H

$! echo HG
echo Goodbye HG
Goodbye HG

Note that the !string syntax will only match commands, not parameters. So !echo H will not execute echo Hello World, but will execute echo Goodbye, and append the parameter H to this command. Similarly, !echo HG is also equivalent to appending HG after the echo Goodbye command.

Because the !string syntax will be expanded to previously executed commands, the string containing ! is placed in double quotation marks, and you must be very careful. If there are non-space characters after it, an error is likely to be reported.

$ echo "I say:\"hello!\""
bash: !\: event not found

The above command will report an error because there is a backslash after the exclamation point. Bash will try to find out whether the command starting with the backslash has been executed before, and an error will be reported if it cannot be found. The solution is to add a backslash before the exclamation mark.

$ echo "I say:\"hello\!\""
I say: "hello\!"

history command

As mentioned earlier, the history command can display the operation history, that is, the contents of the .bash_history file.

$ history

The advantage of using this command instead of directly reading the .bash_history file is that it will add a line number before all operations, the most recent operation is at the end, and the line number is the largest.

By customizing the environment variable HISTTIMEFORMAT, the time of each operation can be displayed.

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

In the above code, %F is equivalent to %Y-%m-%d, and %T is equivalent to %H: %M: %S.

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

The environment variable HISTSIZE sets the number of saved historical operations.

$ export HISTSIZE=10000

The above command is set to save the past 10,000 operation history.

If you don't want to save the history of this operation, you can set HISTSIZE equal to 0.

export HISTSIZE=0

If HISTSIZE=0 is written to the ~/.bashrc file in the user's home directory, then the user's operation history will not be retained. If you write in /etc/profile, the entire system will not retain the operation history.

The environment variable HISTIGNORE can set which commands are not written into the operation history.

export HISTIGNORE='pwd:ls:exit'

In the example above, the three commands pwd, ls, and exit are not written into the operation history.

If you want to search for a previously executed command, you can search the operation history with the grep command.

$ history | grep /usr/bin

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

Each record in the operation history has a number. After knowing the command number, you can execute the command with exclamation mark + number. If you want to execute the eighth command in .bash_history, you can do as follows.

$! 8

The -c parameter of the history command can clear the operation history.

$ history -c

The following are some shortcut keys related to operation history.

  • Ctrl + p: Display the previous command, which has the same effect as the up arrow (previous).
  • Ctrl + n: Display the next command, which has the same effect as the down arrow (next).
  • Alt + <: Display the first command.
  • Alt + >: Display the last command, the current command.
  • Ctrl + o: Execute the current item in the history file and automatically display the next command. This is very helpful for repeating a sequence of commands.

The shortcut keys for the exclamation mark ! are as follows.

  • !!: Execute the previous command.
  • !n: n is a number, execute the command with line number n in the history file.
  • !-n: Execute n commands before the current command.
  • !string: Execute the most recent command starting with the specified string string.
  • !?string: Execute the most recent command containing the string string.
  • !$: Represents the last parameter of the previous command.
  • !*: Represents all the parameters of the previous command, that is, all parts except the command.
  • ^string1^string2: Execute the most recent command containing string1 and replace it with string2.

The following are examples of !$ and !*.

$ cp a.txt b.txt
$ echo !$
b.txt

$ cp a.txt b.txt
$ echo !*
a.txt b.txt

In the above example, !$ represents the last parameter of the previous command (b.txt), and !* represents all the parameters of the previous command (a.txt b.txt).

The following is an example of ^string1^string2.

$ rm /var/log/httpd/error.log
$ ^error^access
rm /var/log/httpd/access.log

In the above example, ^error^access replaces error in the most recent command containing error with access.

If you want to determine what command it is and then execute it, you can turn on the histverify option. In this case, the command generated by using the ! shortcut key will be printed out first, and then executed after the user presses the Enter key.

$ shopt -s histverify

Other shortcut keys

  • Ctrl + j: equivalent to the Enter key (LINEFEED).
  • Ctrl + m: equivalent to CARRIAGE RETURN.
  • Ctrl + o: It is equivalent to the Enter key and displays the next command in the operation history.
  • Ctrl + v: Turn the next special character entered into a literal, such as a carriage return to ^M.
  • Ctrl + [: equivalent to ESC.
  • Alt + .: Insert the last word of the previous command.
  • Alt + _: equivalent to Alt + ..

The above Alt + . shortcut keys are sometimes very convenient for long file paths. Because the last parameter of Unix commands is usually the file path.

$ mkdir foo_bar
$ cd #Press Alt +.

In the above example, pressing Alt + . after the cd command will automatically insert foo_bar.