The use of common commands in vi editor

There are many kinds of text editors under Linux, vi is the most commonly used, and it is also the standard configuration of various versions of Linux. Note that vi is just a text editor that can color characters and autocomplete, but doesn't have typesetting functions like word under Windows.

vi is a product that has been sharpened in ten years. Although there are many commands and most functions are completed by keyboard input, once you are familiar with it, you will find that the functions and efficiency of vi are unmatched by other graphical interface editors.

Vim is the abbreviation of Vi improved, which is an improved version of vi. In Linux, vi is considered the de facto standard editor because:
all versions of Linux come with vi editor; it
takes up less resources;
compared to other editors such as ed, ex, vi is more user-friendly.

You can use the vi editor to edit existing files, create new ones, and open text files in read-only mode.
Entering the vi editor

You can enter the vi editor in the following ways:
Command Description
vi filename If filename exists, open it; otherwise, create a new file and open it.
vi -R filename Open an existing file in read-only mode (view only, not edit).
view filename Opens an existing file in read-only mode.

For example, use the vi editor to create a new file and open it:
$vi testfile
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File] The
vertical bar (|) represents the cursor position; the tilde (~ ) means that the line has nothing. If there is no ~, and nothing is visible, then this line must have whitespace characters (spaces, tab indents, newlines, etc.) or invisible characters.
Working Mode

Learn more about vi Before learning about vi's working mode, vi has three working modes:
1) Normal mode When entering the vi editor

from Shell, first enter the normal mode. In normal mode, any character entered from the keyboard is interpreted as a command. There is no prompt in normal mode, the command is executed immediately after inputting, no carriage return is required, and the inputted characters will not be displayed on the screen.

In normal mode, you can execute commands, save files, move the cursor, paste and copy, etc.
2) Edit mode

Edit mode is mainly used for text editing. Any characters entered by the user in this mode are saved as the contents of the file and displayed on the screen.
3) Command mode

In command mode, users can perform some advanced processing on files. Although commands in normal mode can perform many functions, it is necessary to enter command mode to perform some operations such as string search, replace, and display line numbers.

Note: Some tutorials say that there are two working modes, which is to merge the command mode into the normal mode.

Working mode switching:
Enter the editing mode when inputting i (insert), c (modification), o (starting a new line) command in normal mode; press esc key to return to normal mode.
Enter a colon (:) in normal mode to enter command mode. After inputting the command, press Enter. After the command is executed, it will automatically return to the normal mode.

Tip: If you are not sure which mode you are currently in, pressing the Esc key twice will return to normal mode.
Exiting the vi editor The vi editor is

normally exited in command mode.

Exit command description
q If the file has not been modified, it will directly return to the Shell; otherwise, it will prompt to save the file.
q! Force quit without saving changes.
The wq w command saves the file, and the q command exits vi, which means save and exit.
ZZ save and exit, equivalent to wq, but more convenient.
Before exiting, you can also specify a filename after the w command to save the file as a new file, for example:
w filename2
saves the current file as filename2.

Note: When vi edits a file, the user's operations are based on the copy in the buffer. If you exit without saving to disk, the contents of the buffer will be lost.
Moving the cursor

In order not to affect the contents of the file, the cursor must be moved in normal mode (press the Esc key twice). Use the commands in the following table to move one character at a time:
command description
k move cursor up (move one line)
j move the cursor down (move one line)
h move the cursor to the left (move one character)
l Move the cursor to the right (move one character)
Two reminders:
vi is case-sensitive, be careful not to lock uppercase when entering commands.
You can prefix the command with a number, for example, 2j moves the cursor down two lines.

Of course, there are many other commands to move the cursor, but remember, be sure to be in normal mode (press the Esc key twice).

command to move the cursor
Command description
0 or Position the cursor at the beginning of a line.
$ positions the cursor at the end of a line.
w locates the next word.
b Navigate to the previous word.
(Locate at the beginning of a sentence, and the sentence is defined by three symbols ! . ?
) to the end of a sentence.
{ Move to the beginning of the paragraph. &&&&&&
} Move to end of paragraph. &&&&&&&&&&
[[ Go back to the beginning of the paragraph. &&&&&&&&&&&
]] Move forward to the beginning of the next paragraph. &&&&&&&&&&&
n Move to the nth column (current row).
1G moves to the first line of the file.
G moves to the last line of the file.
nG Move to line n of the file.
:n Move to line n of the file.
H Moves to the top of the screen.
nH Move to the nth line from the top of the screen.
M to move to the middle of the screen.
L Moves to the bottom of the screen.
nL Move to the nth line from the bottom of the screen.
:xx is a number that means move to the line with line number x.


Control Commands

There are some control commands that can be used in combination with the Ctrl key, as follows:
command description
CTRL+d scroll forward half screen
CTRL+f scroll forward full screen
CTRL+u scroll back half screen
CTRL+b scroll back full screen
CTRL+e scroll up one line
CTRL+y scroll down one line
CTRL+I refresh screen

Editing files

Switch to edit mode to edit files. There are many commands to switch from normal mode to edit mode, as follows:
command description
i inserts text before the current cursor position
I insert text at the beginning of the current line
a Insert text after the current cursor position
A inserts text at the end of the current line
o Create a line below the current position
O create a line above the current position

Delete characters

The following commands can delete characters or lines in a file:
Command description
x deletes the character under the current cursor
X deletes the character before the cursor
dw delete characters from the current cursor to the end of the word
d^ delete the character from the current cursor to the beginning of the line
d$ delete the character from the current cursor to the end of the line
D delete the character from the current cursor to the end of the line
dd deletes the line where the current cursor is located

You can add a number prefix in front of the command to indicate the number of times to repeat the operation. For example, 2x means to delete the character under the cursor twice in a row, and 2dd means to delete the line where the cursor is located twice in a row.

It is recommended that readers practice the above commands more and then study them further.
Modifying text

If you wish to modify characters, words or lines, use the following command:
command description
cc deletes the current line and enters edit mode.
cw deletes the current word (word) and enters edit mode.
r Replaces the character under the current cursor.
R Replace characters from the current cursor, press Esc to exit.
s Replaces the current character with the entered character and enters edit mode.
S Replaces the current line with the entered text and enters edit mode.

Paste

the copy-paste command in vi:
command description
yy copy the current line
nyy copy n lines
yw copy a word (word)
nyw copy n lines
p paste the copied text after the cursor
P Paste the copied text in front of the cursor

Advanced Commands

Some of the commands below may look a little weird, but they will make your work more efficient. If you are a heavy vi user, take a look at it.
Command description
J joins the current line and the next line into one line
<< move the current line to the left by one unit (one indent width)
>> move the current line to the right by one unit (one indent width)
~ Change the case of the current character
^G Ctrl+G key combination can display the current file name and status
U undo changes made to the current line
u undo the last operation, press 'u' again to restore the operation
:f Displays the current cursor position in the file, the file name and the total number of lines in the file in the form of a percent sign (%).
:f filename rename file to filename
:w filename save changes to filename
:e filename open another file named filename
:cd dirname Change the current working directory to dirname
:e # switch between two open files
:n If you have multiple files open with vi, you can use :n to switch to the next file
:p If you have multiple files open with vi, you can use :n to switch to the previous file
:N If you have multiple files open with vi, you can use :n to switch to the previous file
:r file reads the file and inserts after the current line
:nr file read file and insert after line n

Text search

If you want to search the whole file, you can enter the / command in the normal mode (press the Esc key twice), then the status bar (the last line) will appear "/" and prompt to enter the string to be searched, press Enter. .

The / command looks down, if you want to look up, you can use the ? command.

At this time, enter the n command to continue searching in the same direction, and enter the N command to continue searching in the opposite direction.

The searched string can contain some characters with special meaning. If you want to search for these characters themselves, you need to add a backslash (\) in front.

Partial list of special characters
character description
^ matches the beginning of a line
. matches a character
* matches 0 or more characters
$ matches the end of a line
[ ] matches a set of characters
If you want to search for a single character on a line, you can use the f or F command, f searches up, F searches down, and will position the cursor at the matching character.

You can also use the t or T commands: the t command searches up and positions the cursor before the matching character; the T command searches down and positions the cursor after the matching character.
set command

The set command can make some settings for the vi editor. Using the set command requires entering command mode.

:set command options
Command description
:set ic Ignore case when searching.
:set ai Set auto-indent (auto-align).
:set noai Cancels auto-indentation (auto-alignment).
:set nu Display line numbers.
:set sw sets the number of spaces to indent, for example, set the number of indent spaces to 4: :set sw=4.
:set ws Loop search: If the specified character is not found until the end of the file, it will go back to the beginning to continue searching.
:set wm Set automatic line wrapping, for example, set a line break when 2 characters away from the margin: :set wm=2 .
:set ro Change the file type to read-only.
:set term Print the terminal type.
:set bf Ignore input control characters, such as BEL (bell), BS (backspace), CR (carriage return), etc.

Run the command

to switch to the command mode, and then enter the ! command to run the Linux command.

For example, before saving a file, if you want to check whether the file exists, enter
: !ls
to list the files in the current directory.

Press any key to return to the vi editor.
Text Replacement

SwitchThe syntax is:
:s/search/replace/g
search is the retrieved text, replace is the text to be replaced, and g means global replacement.
A few tips

The use of the vi editor has been explained, but please remember the following points:
Enter a colon (:) to enter command mode, and press the Esc key twice to enter normal mode.
The meaning of command case is different.
You must be in edit mode to enter content.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327052423&siteId=291194637