English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux vi/vim

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

But at present, we use the vim editor more.

vim has the ability to edit programs, and can actively distinguish the correctness of syntax with font color, which is convenient for program design.

Related articles:The Most Comprehensive Vim Shortcut Key Map — From Beginner to Advanced

What is vim?

Vim is a text editor developed from vi. It has a particularly rich set of convenient programming features such as code completion, compilation, and error jump, and is widely used by programmers.

In simple terms, vi is an old-fashioned word processor, but it is very complete in terms of functions, but there are still places for improvement. Vim can be said to be a very useful tool for program developers.

Even the official website of vim (http://www.vim.org) Even myself says vim is a program development tool rather than a word processing software.

vim keyboard map:

vi/the use of vim

basically vi/vim is divided into three modes, namelyCommand mode (Command mode),Insert mode (Insert mode)andBottom line command mode (Last line mode). The functions of these three modes are respectively:

Command mode:

The user just started vi/vim, enters command mode.

In this state, keystrokes will be recognized by Vim as commands, not input characters. For example, if we press i now, we will not input a character; i is treated as a command.

Here are some commonly used commands:

  • i Switch to insert mode to input characters.

  • x Delete the character at the current cursor position.

  • : Switch to bottom line command mode to input commands on the bottom line.

If you want to edit text: Start Vim, enter command mode, press i, and switch to insert mode.

Command mode only has some basic commands, so you still need to rely on the bottom line command mode to input more commands.

Insert mode

Press i in command mode to enter insert mode.

In input mode, you can use the following keys:

  • character keys and Shift combinationto input characters

  • ENTERto enter a new line

  • BACK SPACEto delete the character before the cursor

  • DELto delete the character after the cursor

  • arrow keysto move the cursor in the text

  • HOME/ENDto move the cursor to the beginning of the line/end of line

  • Page Up/Page Downto scroll up/scroll down

  • Insertto switch the cursor to input/replace mode, the cursor will become a vertical line/underline

  • ESCto exit input mode and switch to command mode

底线 command mode

Press: (English colon) to enter the底线 command mode.

The底线 command mode can input a single or multiple character commands, and there are many available commands.

In the底线 command mode, the basic commands are (the colon has been omitted):

  • q Exit program

  • w Save file

Press the ESC key at any time to exit the底线 command mode.

Simply put, we can think of the following icons to represent these three modes:

vi/vim Usage Examples

Using vi/vim enters normal mode

If you want to use vi to create a file named w3When editing the file codebox.txt, you can do it this way:

$ vim w3codebox.txt

Directly input vi Filename You can enter vi's normal mode. Please note, remember to add the filename after vi, regardless of whether the file exists or not!

Press i to enter input mode (also known as edit mode) and start editing text

In normal mode, just press i, o, a, etc. to enter input mode!

In edit mode, you can find that –INSERT will appear in the status bar at the bottom left.- This indicates that you can input any character.

At this time, the keyboard except Esc In addition to this key, all other keys can be treated as general input keys, so you can perform any editing.

Press the ESC button to return to normal mode

Alright, assuming I have finished editing it according to the style above, then how should I exit? Yes! That's right! Just press Esc This button! You will soon find that the – INSERT – at the bottom left of the screen has disappeared!

Press :wq Save and leave vi

OK, we need to archive, and the command to save and leave is very simple, input :wq You can save and leave now!

OK! In this way, we have successfully created a w3the file codebox.txt.

vi/vim Key Explanation

In addition to the simple examples of i, Esc, and :wq mentioned above, vim actually has a very large number of keys that can be used.

Part 1: General mode cursor movement, copy and paste, search and replace, and other functions available

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 (↓)Move the cursor one character down
k or the up arrow key (↑)Move the cursor one character up
l or the right arrow key (→)Move the cursor one character to the right
If you place your right hand on the keyboard, you will find that hjkl are arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, to move down 30 lines, you can use "33The combination key '0↓"', That is, add the desired number of times and press the action!
[Ctrl] + [f]The screen moves down one page, equivalent to the [Page Down] key (commonly used)
[Ctrl] + [b]The screen moves up one page, equivalent to the [Page Up] key (commonly used)
[Ctrl] + [d]The screen moves down half a page
[Ctrl] + [u]The screen moves up half a page
+Move the cursor to the line below the non-space character
-Move the cursor to the line above the non-space character
n<space>That 'n' represents 'number', for example 20 . Press the number key and then the space bar, the cursor will move right n characters on this line characters. For example 20<space> will move the cursor to the right 20 characters away.
0 or function key [Home]This number '0' moves to the first character of this line (commonly used)
$ or function key [End]Move to the end of this line (commonly used)
HThe cursor moves to the first character of the top line of this screen
MThe cursor moves to the first character of the central line of this screen
LThe cursor moves to the first character of the bottom line of this screen
GMove to the last line of this file (commonly used)
nGn is a number. Move to the nth line of this file. For example 20G will move to the first line of this file 20 line (can be used with :set nu)
ggMove to the first line of this file, which is equivalent to 1G ah! (commonly used)
n<Enter>n is a number. The cursor moves down n lines (commonly used)
Search and Replace
/wordSearch for a string named 'word' below the cursor. For example, to search for 'vbird' in a file For this string, input /Enter 'vbird'! (commonly used)
?wordSearch for a string named 'word' above the cursor.
nThis 'n' is an English keyboard key. It represents repeating the previous search action. For example, If we just executed /Search downward for the string 'vbird'. Pressing 'n' will continue to search for the next string named 'vbird'. string. If it is executing ?vbird, then pressing n will continue to search for the string named vbird upwards!
NThis N is an English key. It is exactly opposite to n, for 'reverse' to perform the previous search action. For example /vbird, press N then it means 'up' to search vbird.
Use /word combined with n and N is very helpful! It can help you repeatedly find some keywords you are searching for!
:n1,n2s/word1/word2/gn1 and n2 is a number. In the n1 and n2 lines between search word1 this string, and replace the string with word2 ! For example, in 100 to 200 lines between search vbird and replace it with VBIRD then:
『:100,200s/vbird/VBIRD/g』. (commonly used)
:1$s/word1/word2/g or :%s/word1/word2/gSearch for word from the first line to the last line1 string, and replace the string with word2 ! (commonly used)
:1$s/word1/word2/gc or :%s/word1/word2/gcSearch for word from the first line to the last line1 string, and replace the string with word2 ! And display a prompt character to the user for confirmation (confirm) Do you need to replace! (commonly used)
Delete, copy, and paste
x, XIn a line of text, x is to delete one character backward (equivalent to [del] key), X is to delete one character forward (equivalent to [backspace] or the backspace key) (commonly used)
nxn is a number, delete n consecutive characters. For example, I want to delete 10 characters, 『10』.
ddDelete the entire line where the cursor is (commonly used)
nddn is a number. Delete the n lines below the cursor, for example 20dd is to delete 20 lines (commonly used)
d1GDelete all data from the cursor to the first line
dGDelete all data from the cursor to the last line
d$Delete the character at the cursor to the last character of the line
d0That is the number 0, delete the character at the cursor to the first character of the line
yyCopy the line where the cursor is (commonly used)
nyyn is a number. Copy the n lines below the cursor, for example 20yy is to copy 20 line (commonly used)
y1GCopy the line where the cursor is to the beginning of all lines
yGCopy the line where the cursor is to the end of all lines
y0Copy all data from the character at the cursor to the beginning of the line
y$Copy all data from the character at the cursor to the end of the line
p, Pp is to paste the copied data below the cursor, while P is to paste it above the cursor! For example, the cursor is currently at the 20 lines, and it has been copied 10 If there is data, press p after that. that 10 line data will be pasted on the original 20 line after, that is, from 21 line starts pasting. But if you press P? Then the original 20 line will be pushed to become 30 line. (commonly used)
JCombine the data of the current cursor line and the next line into the same line
cRepeat delete multiple data, such as downward delete 10 line, [ 10cj ]
uUndo the previous action (commonly used)
[Ctrl]+rRedo the previous action (commonly used)
This u with [Ctrl]+r is a very commonly used command! One is to undo, and the other is to redo! With these two functional keys, your editing, hehe! It's very pleasant!
Don't doubt it! This is the decimal point! It means to repeat the previous action. If you want to repeat delete, repeat paste, and other actions, just press the decimal point '•'! (commonly used)

Second part: Button descriptions for switching from general mode to editing mode

Enter input or replace editing mode
i, IEnter input mode (Insert mode):
i is 'starting from the current cursor position', I is 'starting from the first non-space character of the current line'. (commonly used)
a, AEnter input mode (Insert mode):
a is 'starting from the next character of the current cursor position', A is 'starting from the last character of the cursor line' (commonly used)
o, OEnter input mode (Insert mode):
This is the case with the English letter o in uppercase and lowercase. o is to input a new line below the current cursor position; O to input a new line above the current cursor position (commonly used)!
r, REnter replace mode (Replace mode):
r will only replace the character at the cursor once; R will continue to replace the text at the cursor until you press ESC stop; (commonly used)
Among these keys, in the lower left corner of the vi screen, there will be a『--INSERT--』or『--REPLACE--』 As the name suggests, this action!! Especially note that we also mentioned above, when you want to input characters into the document, You must see INSERT or REPLACE at the lower left corner to input, otherwise!
[Esc]Exit editing mode and return to general mode (commonly used)

Third part: Button descriptions for switching from general mode to command line mode

Command line storage, exit, and other instructions
:wWrite the edited data to the hard disk file (commonly used)
:w!If the file attribute is 'read-only', force write to the file. However, whether you can write or not, It's still related to the file permissions of the document!
:qExit vi (commonly used)
:q!If you have modified a file and don't want to save it, use ! to force exit without saving the file.
Pay attention, the exclamation mark (!) often means 'force' in vi~
:wqExit after saving, if :wq! is used, it will be forced to save and exit (commonly used)
ZZThat's the capital Z! If you have made changes, save the current file and then exit! Equivalent to (save and exit)
ZQDo not save and force exit. Equivalent to :q!
:w [filename]Store the edited data into another file (similar to save as)
:r [filename]Read another file's data into the editing data. That is, to『filename』 This file content is added to the line behind the cursor
:n1,n2 w [filename]Move n1 to n2 is stored in the file filename.
:! commandTemporarily leave vi to execute command in command line mode and display the result! For example
『:! ls /『home』can be viewed in vi /The file information output by ls under home!
Changes in vim environment
:set nuShow line numbers, after setting, the line number will be displayed before each line
:set nonu相反 to set nu, to cancel line numbers!

Pay special attention to vi/In vim, numbers are very meaningful! Numbers usually mean doing something repeatedly! They may also represent going to the nth something.

For example, to delete 50 line, then use『50dd』, right! Numbers are added before actions, such as moving down 20 lines? That is『20j』or『20↓』can be done.