Vim program editor

1. Use of vi

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

- Normal mode:

  Opening a file with vi directly enters normal mode, which is the default mode.

- Edit Mode:

  In normal mode, you can delete, copy, paste, etc., but you cannot edit the content of the file. Press【i, I,o, O,a,A,r,R】to enter the edit mode. Usually in linux, when these keys are pressed, the words Insert or Replace will appear at the bottom left of the screen, and editing can be done only at this time. If you return to the normal mode, you must press the [Esc] key to exit the editing mode.

- Command line command mode:

  In normal mode, input any one of the three buttons [:/?] to move the cursor to the bottom line. In this mode, you can provide the action of searching for data, and reading, saving, replacing a large number of characters, leaving vi, displaying line numbers, etc. are achieved in this mode.

 

2. Button Description

 

2.1 Description of buttons available in normal mode, cursor move, copy and paste, search and replace

 

2.1.1 The method of cursor movement

ctrl + f : the screen moves down one page, equivalent to the Page Down button

ctrl + b : the screen moves up one page, which is equivalent to the Page Up button

0 or function key [Home]: move to the front character of this line

$ or function key【End】: Move to the last character of this line

G: move to the last line of this file

gg: move to the first line of this file

nG:n is a number. move to line n of this file

n<Enter>: n is a number. Move the cursor down n lines

 

2.1.2 Search and Replace

/word: Find a string named word under the cursor.

? word: Find a string named word above the cursor.

n: This n is the English key. Represents the action of repeating the previous search.

N: This N is the English button. Contrary to n, the previous search action is performed in reverse.

: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, search for vbird between lines 100 and 200 and replace it with VBIRD, then:

:100,200s/vbird/VBIRD/g

:1,$s/word1/word2/g: Find the word1 string from the first line to the last line, and replace the string with word2.

: 1,$s/word1/word2/gc: Find the word1 string from the first line to the last line, and replace the string with word2. Before the replacement, a prompt character is displayed to the user to confirm whether the replacement is required.

 

2.1.3 Delete, copy and paste

x,X: In a line of words, x is to delete one character backward, and X is to delete one character forward.

nx: n is a number, delete n characters in a row.

dd: Delete the entire row where the cursor is located.

ndd: n is a number. Deletes the n lines down where the cursor is located.

d1G: delete all data from the cursor to the first line

dG: delete all data from the cursor to the last line

d0: delete where the cursor is, to the first character of the line

d$: delete where the cursor is, to the last character of the line

yy: copy the line where the cursor is located

nyy: n is a number. Copy down n lines where the cursor is located.

y1G: Copy all data from the cursor line to the first line

yG: Copy all data from the cursor line to the last line

y0: Copy the character where the cursor is located to all the data at the beginning of the line

y$: Copy the character where the cursor is located to all the data at the end of the line

p,P: p means paste the copied data on the line below the cursor, P means paste it on the line above the cursor

J: Combine the data of the row where the cursor is located and the data of the next row into the same row

c: delete multiple data repeatedly, such as delete 10 rows down, 10cj

u: undo the previous action

ctrl + r: redo the last action

.: Repeats the previous action.

 

2.2 Description of available buttons for switching from general mode to edit mode

 

2.2.1 Enter insert mode

i,I: i is to insert from the current cursor position, I is to start the insertion at the first non-space character of the current line

a,A: a means to insert from the next character where the cursor is currently located, A means to insert from the last character of the line where the cursor is located

o,O: o is to insert a new line at the next line where the cursor is currently located; O is to insert a new line at the previous line where the current cursor is located

 

2.2.2 Enter Replacement Mode

r,R: r will only replace the character under the cursor once; R will always replace the text under the cursor until Esc is pressed

 

2.3 Instructions for storing and leaving the instruction sequence

:w : write the edited data to the hard disk file

:w! : Forcibly write to the file if the file attribute is read-only. However, whether it can be written or not is still related to the file permissions of the file.

:q : leave vi

:q! :If you have modified the file and don't want to save it, use it! Archives are not stored for forced exit.

:wq : save and leave, if it is :wq!, it is forced to save and leave

ZZ : This is a capital Z. If the file is not changed, it will not be saved and left; if the file has been changed, it will be saved and left.

:w [filename] : save edited data as another file

:r [filename] : In the edited data, read the data of another file. That is, the filename file content is added 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 display the result of executing command in command line mode  

 

3. Extra features of vim

 

3.1 Block selection

Button meaning of block selection

v: character selection, the place where the cursor passes will be highlighted and selected

V: line selection, the line the cursor passes through will be highlighted and selected

[Ctrl]+v: block selection, you can use a rectangle to select

y: Copy the highlighted place

d: delete the highlighted place

 

3.2 Editing multiple files

We know that vi can use :r filename to read the contents of a file, but after all, this is to read the entire file. If you only want part of the content, this is useful for editing multiple files at the same time.

Keys for editing multiple files:

:n : Edit next file

:N : Edit the previous file

:files : List all files currently open in this vim 

 

3.3 Multi-window function

Two files are displayed on one screen at the same time, and the key function in the case of multiple windows

:sp [filename] : Open a new window, if there is a filename, it means to open a new file in the new window, otherwise it means that the two windows are the same file content

Ctrl + w + ↓ : Key method: first press ctrl and hold, then press w, then release all the keys, and then press ↓ or j, the cursor can move to the window below.

Ctrl + w + ↑ : move the cursor to the window above

Ctrl + w + q : leave

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326289732&siteId=291194637