Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Linux vi/vim


May 22, 2021 Linux


Table of contents


All Unix Like systems have built-in vi instrument editors, while other instrument editors do not necessarily exist.

But at the moment we're using more vim editors.

Vim has the ability of program editing, can actively identify the correctness of grammar by font color, convenient program design.


What is vim?

Vim is a text editor developed from vi. T he features of easy programming, such as code replacement, compilation and error jump, are particularly rich and are widely used in programmers.

Simply put, vi is an old-fashioned word processor, but it's fully functional, but there's room for improvement. V im can be said to be a very useful tool for developers. E ven vim's official website http://www.vim.org itself says vim is a program development tool, not word processing software.

Vim keyboard diagram:

Linux vi/vim


vi/vim usage

Basically vi/vim is divided into three modes: Command mode, Insert mode, and Last line mode. T he roles of these three models are:

Command mode:

As soon as the user starts vi/vim, they enter command mode.

Tapping the keyboard action in this state is recognized by Vim as a command, not as an input character. F or example, if we press i at this time, we don't enter a character, i is treated as a command.

Here are a few common commands:

  • i Switch to input mode to enter characters.
  • x Removes the character where the current cursor is located.
  • : Switch the inline command mode to enter the command on the bottom line.

To edit text: Start Vim, enter command mode, press i, switch to input mode.

Command mode has only some of the most basic commands, so you still rely on bottom-line command mode to enter more commands.

Enter the mode

Pressing i in command mode enters input mode.

In input mode, you can use the following keys:

  • Character keys and Shift combinations, enter characters
  • ENTER, enter key, line change
  • BACK SPACE, back in the bar, removes the previous character of the cursor
  • DEL, remove the key, remove the last character of the cursor
  • Arrow key to move the cursor through the text
  • HOME/END, move the cursor to the beginning/end of the line
  • Page Up/Page Down, top/bottom page
  • Insert, switch the cursor to input/replace mode, and the cursor becomes vertical/underlined
  • ESC, exit input mode and switch to command mode

Bottom line command mode

Press in command mode: (English colon) enters bottom-line command mode.

The bottom-line command mode can enter commands with a single or more characters, and there are many commands available.

In bottom-line command mode, the basic commands are (colons have been omitted):

  • q Exit the program
  • w Save the file

Press the ESC key to exit bottom-line command mode at any time.

Simply put, we can think of these three patterns as the icons below:

Linux vi/vim

Vi/vim uses instances

Use vi/vim to enter command mode

If you want to use vi to build a file called .txt, you can do this:

[root@www ~]# vi test.txt

Enter the vi file name directly to enter the command mode of vi. P lease note that remember to add the file name after vi, whether the file exists or not!

Linux vi/vim

Press i to enter mode and start editing the text

In command mode, just press i, o, a and other characters to enter mode!

In input mode, you can see that the word "INSERT" appears in the status bar in the lower left corner, which is a hint that you can enter any character.

At this point, all the keys on the keyboard can be viewed as normal input buttons, so you can make any edits.

Linux vi/vim

Press the button to return to command mode

Well, let's say I've edited him in the style above, so how do I quit? T hat's right! T hat's right! J ust press the button for him to press the button! R ight away you'll find that the bottom left corner of the screen - INSERT - is gone!

Press :wq in command mode to save and leave vi

OK, we're going to archive it, the instruction to save and leave is very simple, enter ":wq" to save the leave!

Linux vi/vim

Ok! S o we successfully created a .txt file. Isn't it very simple?


Vi/vim key description

In addition to the simple example above of i, Esc, : wq, vim actually has a very large number of keys to use.

Part 1: Button description available for command mode, cursor movement, copy sticker, search for replacement, etc

The method of moving the cursor
h or left arrow key (←) The cursor moves one character to the left
j or down arrow keys (s) The cursor moves one character down
k or up arrow keys (') The cursor moves one character up
l or right arrow key (→) The cursor moves one character to the right
If you put your right hand on the keyboard, you'll find that hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, such as moving 30 lines down, you can use the "30j" or "30" combination button, i.e. add the number of times you want to do (numbers), press the action!
[Ctrl] + [f] The screen moves one page "down" to the equivalent of the Page Down button (commonly used)
[Ctrl] + [b] The screen moves one page "up" to the equivalent of the Page Up button (commonly used)
[Ctrl] + [d] The screen moves half a page "down"
[Ctrl] + [u] The screen moves half a page up
+ The cursor moves to the next column of non-space characters
- The cursor moves to the last column of a non-space character
N That n means "number", for example, 20. P ress the number and then press the space bar, and the cursor moves n characters of the line to the right. For example, 20, the cursor moves 20 characters behind it.
0 or function keys (Home) This is the number '0': move to the front character of this line (commonly used)
$ or function keys (End) Move to the last face character of this line (commonly used)
H The cursor moves to the first character on the top line of the screen
M The cursor moves to the first character on that line in the center of the screen
L The cursor moves to the first character on the bottom line of the screen
G Move to the last line of this file (commonly used)
nG n is the number. M ove to line n of this file. For example, 20G moves to line 20 of this file (matchable :set nu)
Gg Move to the first line of this archive, equivalent to 1G ah! (Commonly used)
N n is the number. Cursor moves down n lines (commonly used)
Search and replace
/word Look under the cursor for a string with the name word. F or example, to search for the vbird string in your archive, enter /vbird! (Commonly used)
?word Look above the cursor for a string with the word name.
N This n is the English button. R epresents a repetition of the previous search. F or example, if we just execute /vbird to search down for the vbird string, press n and continue to search down for the next string with the name vbird. If you are executing ?vbird, pressing n will continue to search up for a string with the name vbird!
N This N is an English button. I n contrast to n, the previous search action is performed for Reverse. For example, after /vbird, pressing N means "up" in search of vbird.
Using /word to mate with n and N is very helpful! Allows you to find some keywords you're looking for over and over again!
:n1,n2s/word1/word2/g n1 and n2 are numbers. L ook for the word1 string between lines n1 and n2 and replace it with word2! For example, search for vbird between 100 and 200 lines and replace it with VBIRD:
『:100,200s/vbird/VBIRD/g』。 (Commonly used)
:1,$s/word1/word2/g Look for the word1 string from the first line to the last line and replace it with word2! (Commonly used)
:1,$s/word1/word2/gc Look for the word1 string from the first line to the last line and replace it with word2! A nd show the prompt character to confirm to the user before the replacement (confirm) whether it needs to be replaced! (Commonly used)
Delete, copy, and paste
x, X In a line of words, x removes one character backward (equivalent to the "del" button) and X removes one character forward (equivalent to "backspace" or backspace) (commonly used)
Nx n is a number, with n characters removed consecutively back. For example, I'm going to delete 10 characters in a row, "10x."
Dd Delete the entire line where the cursor is located (commonly used)
ndd n is the number. Delete the next n row where the cursor is located, for example 20dd deletes 20 rows (commonly used)
d1G Delete all data from the cursor to the first row
Dg Delete all data from the cursor to the last row
d$ Delete the cursor to the last character on the line
d0 That's the number 0, remove the cursor from its place, and go to the first character of the line
Yy Copy the line where the cursor is located (commonly used)
nyy n is the number. Copy the next n column where the cursor is located, for example 20yy is copy 20 columns (commonly used)
y1G Copy all the data from the column to the first column where the cursor is located
yG Copy all the data in the column to the last column where the cursor is located
y0 Copy all the data from the character where the cursor is located to the top of the line
y$ Copy all the data from the character where the cursor is located to the end of the line
p, P p To paste the copied data on the next line of the cursor, P is to stick to the line on the cursor! F or example, I am currently cursor on line 20 and have copied 10 rows of data. W hen p is pressed, the 10 rows of data are posted after the original 20 rows, i.e. from the 21 rows. B ut what if you press P? T hen the original line 20 is pushed to 30. (Commonly used)
J Combines the column where the cursor is located with the data in the next column into the same column
C Repeated deletion of multiple data, e.g. 10 rows down, .
u Restore the previous action. (Commonly used)
[Ctrl]+r Redo the last action. (Commonly used)
This u is a very commonly used instruction with the instructions of the "Ctrl"r! O ne is recovery, the other is to redo once - use these two function buttons, your editor, hey hey! Very happy!
. Don't doubt it! T hat's the number of points! I t means repeating the meaning of the previous action. I f you want to repeat deletions, repeat sticks, and so on, press the tap. I 'll be fine! (Commonly used)

Part 2: Instructions for the available buttons for the command mode to switch to input mode

Enter or replace the edit mode
i, I Enter insert mode:
i is Enter from where the current cursor is located, and I is Enter from the first non-space character on the current line. (Commonly used .) )
a, A Enter insert mode:
A is "Enter from the next character where the cursor is located" and A is "Enter from the last character on the line where the cursor is located." (Commonly used .) )
o, O Enter insert mode:
This is the case of the English letter o. o Enter a new line at the next line where the current cursor is located, O enter a new line at the next line where the current cursor is located! (Commonly used .) )
r, R Enter Replace mode:
r replaces only the character in which the cursor is located once; R replaces the text in which the cursor is located until the ESC is pressed; (commonly used). )
In these buttons above, the words "--INSERT--" or "-REPLACE--" appear in the lower left corner of the vi screen. B y the name to know the action! ! In particular, we mentioned above, when you want to enter characters in the file, be sure to see INSERT or REPLACE in the lower left corner to enter Oh!
[Esc] Exit edit mode and return to command mode (commonly used). )

Part III: Description of the available buttons for command mode toggle the inline command mode

Bottom-line command mode storage, departure, etc. instructions
:w Write edited data to the hard disk archive (commonly used). )
:w! If the file property is Read-Only, force writing to the file. However, in the end can be written, or with you on the archives of the file permission ah!
:q Leave vi (commonly used). )
:q! If you have modified the file, and do not want to store, use! Do not store files for forced departure.
Notice that the exclamation point (!) often means "force" in vi
:wq After storage and leave, if : wq! Is forced to leave after storage (commonly used). )
Zz This is a capital Z! If the file has not been moved, then do not store away, if the file has been moved, then store and leave!
:w [filename] Store edited data in another archive (similar to saving a new file)
:r [filename] In the edited data, read the data from another archive. Also add the archive of "filename" to the line where the cursor is located
:n1,n2 w [filename] Store the contents of n1 to n2 as filename.
:! command Temporarily leave vi to execute the display result of the command in instruction column mode! For example
『:! ls /home" you can see the archive information output with ls under /home in vi!
Changes to the vim environment
:set nu The line number is displayed, and when set, the line number for each line is displayed in the prefix
:set nonu In contrast to set nu, cancel the line number!

In particular, in vi/vim, numbers make sense! N umbers usually mean repeating it several times! It may also mean that the representative goes to the first few or what.

For example, to delete 50 lines, use "50dd" right! T he number is added before the action, what if I want to move 20 lines down? T hat's "20j" or "20".