Linux vi/vim


All Unix Like systems will have a built-in vi document editor, but other document editors may not exist.

But currently we use the vim editor more.

vim has the ability to edit programs, and can actively identify the correctness of syntax by font color, which facilitates program design.

Related articles: The most complete Vim shortcut key bitmap in history—from entry to advanced


What is vim?

Vim is a text editor developed from vi. It is particularly rich in functions that facilitate programming, such as code completion, compilation, and error jumping, and is widely used among programmers.

To put it simply, vi is an old-fashioned word processor, but its functions are already very complete, but there is still room for improvement. vim can be said to be a very useful tool for program developers. Even vim's official website (http://www.vim.org) itself says that vim is a program development tool rather than a word processing software.

vim keyboard diagram:

vi-vim-cheat-sheet-sch.gif


Usage of vi/vim

Basically, vi/vim is divided into three modes , which are general mode, editing mode and command line command mode respectively. The functions of these three modes are:

  • General mode:
    Opening a file with vi will directly enter the normal mode (this is the default mode). In this mode, You can use the "up, down, left, and right" keys to move the cursor, and you can use "delete characters" or "delete entire lines" to process file contents. You can also use "copy and paste" to process your file data.

  • Edit mode:
    In the normal mode, you can delete, copy, paste, etc., but you cannot edit the file content! Wait until you press 『i, I, o, O, a, A, r, R, etc. will enter the editing mode after any letter. Attention! usually in In Linux, when these keys are pressed, "INSERT" will appear in the lower left corner of the screen. or REPLACE‖, only then can you edit. And if you want to return to normal mode, You must press the "Esc" button to exit the edit mode.

  • Command line command mode:
    In normal mode, enter " : / ? ‖ Any one of the three buttons can move the cursor to the bottom line. In this model, It can provide you with the actions of "searching data", including reading, saving, replacing a large number of characters, and exiting. The actions of vi, displaying line numbers, etc. are achieved in this mode!

To put it simply, we can think of these three modes as the icons below:

1040.png

vi/vim usage examples

Use vi/vim to enter normal mode

If you want to use vi to create a file named test.txt, you can do this:

[root@www ~]# vi test.txt

Directly entervi file nameto enter the normal mode of vi. Please note that you must add the file name after vi, regardless of whether the file exists or not!

1041.png

Press i to enter the editing mode and start editing text

In the normal mode, just press i, o, a and other characters to enter the editing mode!

In edit mode, you can find the word –INSERT- appearing in the status bar in the lower left corner, which is a prompt that you can enter any characters.

At this time, except for the [Esc] key, all other keys on the keyboard can be regarded as general input buttons, so you can perform any editing.

1048.jpg

Press the [ESC] button to return to the normal mode

Okay, assuming I have finished editing it according to the above style, how should I exit? Yes! That's right! Just press the [Esc] button for him! Immediately you will find that - INSERT - in the lower left corner of the screen is missing!

In normal mode, press :wq to save and leave vi

OK, we are going to save, the command to save and leave is very simple, enter ":wq" to save and leave!

1049.png

OK! In this way, we successfully created a test.txt file. Is not it simple.


vi/vim key description

In addition to i, [Esc], :wq in the simple example above, there are actually many keys that can be used in vim.

Part 1: Description of buttons available in normal mode, cursor movement, copy and paste, search and replace, etc.

##n That n means " Number", such as 20. After pressing the number and then pressing the space bar, the cursor will move n to the right on this line characters. For example, 20 will move the cursor 20 characters back. 0 or function key [Home] This is the number "0": move to the front character of this line (Commonly used) $ or function key [End] Move to the last character of this line (Commonly used) H The cursor moves to the first character of the top line of the screen M The cursor moves to the center of the screen The first character of that line L Move the cursor to the first character of that line at the bottom of the screen G Move to the last line of this file (commonly used) nG n is a number. Move to line n of this file. For example, 20G will be moved to the 20th location of this file. Line (can be used with :set nu) gg Move to the first line of this file, which is equivalent to 1G! (Commonly used) n n is a number. Move the cursor down n lines (commonly used) ##Search and replace /word ?word
Methods to move the cursor
h or the left arrow key (←) Move the cursor one character to the left
j or the down arrow key Key (↓) The cursor moves down one character
k or the up arrow key (↑) The cursor moves up one character
l or right arrow key (→) Move the cursor one character to the right
If you place your right hand on the keyboard, you'll notice that hjkl is arranged together so that you can use the four buttons to move the cursor. If you want to move multiple times, for example, move down 30 lines, you can use the "30j" or "30↓" key combination. That is, after adding the number of times (number) you want to perform, press the action!
[Ctrl] + [f] Move the screen "down" one page, equivalent to the [Page Down] button (Commonly used)
[Ctrl] + [b] Move the screen "up" one page, equivalent to the [Page Up] button (Commonly used)
[Ctrl] + [d] Move the screen "down" by half a page
[Ctrl ] + [u] Move the screen "up" half a page
+ Move the cursor to the next column of non-space characters
- Move the cursor to the previous column of non-space characters
Look for a string named word under the cursor. For example, to search for vbird in a file Just enter /vbird for this string! (Commonly used)
Look for a string named word above the cursor.
n This n is the English button. Represents the action of repeating the previous search. for example, If we just executed /vbird to search downwards for the string vbird, then after pressing n, we will continue to search downwards for the next name vbird. String. If ?vbird is executed, then pressing n will continue to search upward for the string named vbird!
N This N is the English button. Just the opposite of n, it is "reverse" to perform the previous search action. For example, after /vbird, pressing N means "up" to search for vbird.
Using /word with n and N is very helpful! It allows you to repeatedly find some of the keywords you search for!
:n1,n2s/word1/word2/g n1 and n2 are numbers. Find the string word1 between lines n1 and n2 and replace the string with word2! For example, searching for vbird between lines 100 and 200 and replacing it with VBIRD would be:
『:100,200s/vbird/VBIRD/g』. (Commonly used)
:1,$s/word1/word2/g Find the word1 string from the first line to the last line and replace the string with word2! (Commonly used)
:1,$s/word1/word2/gc Find the word1 string from the first line to the last line and replace it with the string for word2! And display prompt characters to the user to confirm before replacing Does it need to be replaced? (Commonly used)
Delete, copy and paste
x, X in one line Among them, x is to delete one character backward (equivalent to the [del] key), X is to delete one character forward (equivalent to [backspace], which is the backspace key) (Commonly used)
nx n is a number, and n characters are deleted consecutively. For example, I want to delete 10 characters in a row, "10x".
dd Delete the entire row where the cursor is (commonly used)
ndd n is a number. Delete n columns downward where the cursor is located. For example, 20dd deletes 20 columns. (Commonly used)
d1G Delete all data from the cursor to the first line
dG Delete all data from the cursor to the last line
d$ Delete from the cursor to the last character of the line
d0 That is the number 0, delete where the cursor is, go to the first character of the line
yy Copy The line where the cursor is located (commonly used)
nyy n is a number. Copy n columns downward where the cursor is located, for example, 20yy copies 20 Column (commonly used)
y1G Copy all data from the column where the cursor is to the first column
yG Copy all data from the column where the cursor is to the last column
y0 Copy all data from the character where the cursor is to the beginning of the line
y$ Copy all the data from the character where the cursor is to the end of the line
p, P p is to paste the copied data on the line below the cursor, and P is to paste it on the line above the cursor! For example, my cursor is currently on row 20, and 10 rows of data have been copied. Then after pressing p, Those 10 rows of data will be pasted after the original 20 rows, that is, starting from row 21. But what if you press P? Then the original line 20 will be pushed to line 30. (Commonly used)
J Combines the data in the column where the cursor is and the next column into the same column
c Repeatedly delete multiple data, for example, delete 10 rows downward, [ 10cj ]
u Restore the previous action. (Commonly used)
[Ctrl]+r Redo the previous action. (Commonly used)
This u and [Ctrl]+r are very commonly used commands! One is restoration, the other is redoing~ Use these two function keys for your editing, hehe! Very happy!
. do not doubt! This is the decimal point! It means to repeat the previous action.If you want to delete repeatedly, paste repeatedly, etc., just press the decimal point "."! (Commonly used)

Part 2: Available button descriptions for switching from normal mode to edit mode

Enter the insert or replace editing mode
i, I Enter the insert mode (Insert mode):
i means "Insert from the current cursor position", I means "Insert from the first non-space character on the current line". (Commonly used)
a, A Enter Insert mode:
a means "start inserting from the next character where the cursor is currently located", A It is "Insert starting from the last character of the line where the cursor is located". (Commonly used)
o, O Enter Insert mode:
This is the uppercase and lowercase version of the English letter o. o means "Insert a new line at the next line where the cursor is currently located"; O Insert a new line above the current cursor position! (Commonly used)
r, R Enter Replace mode:
r will only replace the character where the cursor is located once; R will always replace the character where the cursor is located until the ESC; (Commonly used)
Among the above keys, "--INSERT--" or "--REPLACE" will appear in the lower left corner of the vi screen --‖ words. You can already tell this action from the name! ! Special note is that, as we mentioned above, when you want to enter characters in the file, You must see INSERT or REPLACE in the lower left corner to enter!
[Esc] Exit editing mode and return to normal mode (Commonly used)

Part 3: Description of available buttons for switching from normal mode to command line mode

Command bar save, exit and other commands
:w Write the edited data to the hard disk file (Commonly used)
: w! If the file attribute is "read-only", the file is forced to be written. However, can it be written? It still has something to do with your file permissions on the file!
:q Leave vi (commonly used)
:q! If you have modified the file and do not want to save it, use ! to force leaving without saving the file.
Pay attention, the exclamation point (!) in vi often has the meaning of "force"~
:wq Save and leave. If it is:wq!, it is forced to save and leave. (Commonly used)
ZZ This is a capital Z! If the file has not been modified, then leave without saving. If the file has been modified, save and then leave!
:w [filename] Save the edited data into another file (similar to saving a new file)
:r [filename] In the edited data, read the data of another file. That is, "filename" The content of this file is added to the end of the line where the cursor is located
:n1,n2 w [filename] Save the contents from n1 to n2 into the file filename.
:! command Temporarily leave vi and execute command in command line mode to display the results! For example
『:! ls /home』You can view the file information output by ls under /home in vi!
vim environment changes
:set nu displays the line number, after setting , the line number of the line will be displayed in the prefix of each line
: set nonu is the opposite of set nu, which is to cancel the line number!

Specially note that in vi/vim, numbers are very meaningful! The number usually represents how many repetitions it means! It may also mean the number you are going to or something.

For example, if you want to delete 50 rows, use "50dd", right? The number is added before the action, what if I want to move down 20 rows? That is "20j" or "20↓".