Vi operation memo

Vi operation memo

      Vi is a screen editing program in the Linux system, used in the terminal. The modification of the file by Vi is carried out on the copy of the file. Unless the editing is successful and the original file is replaced by the modified copy after saving, the modified content will be discarded and the original file will be returned.

      Vi is an essential skill for a good code engineer. Therefore, the vi memo is briefly described here .

1. You can enter the following command line on the terminal to enter the vi editing program:

      vi filename

      vi has three modes of operation, namely edit mode, insert mode and command mode.

2. Edit mode

The main purpose of editing mode is to move the cursor position in the edited file.

2.1

move up                           j     move down

h    move left                            l     move right

ctrl+f   move forward one page in the file

ctrl+b  move back one page in the file

H   moves the cursor to the starting line on the screen

to move the cursor to the middle of the screen

L    move the cursor to the last line of the screen

Numbers can also be added in front of the H and L commands to indicate the number of lines moved into the screen. For example , 3H means move to the second line

w Moves the cursor right to the beginning of the next line

eMove the cursor right to the end of a word

b move the cursor left to the beginning of the previous word

0 (zero) moves the cursor left to the beginning of the line

^ The cursor moves to the first non-blank character in the navigation

$ move the cursor right to the end of the line

2.2 , search string

/string searches forward for the given string string

? string searches backwards for the given string string

n Search forward or backward to find the next occurrence of the string (when reaching the end or beginning of the file, he will bypass to the other end of the file and start searching from there)

2.3 , replacement and deletion

rc replaces the character indicated by the current cursor with c

x deletes the character at the current cursor position

dw delete the word to the right of the cursor

db delete the word before the cursor

dd deletes the line where the cursor is, and removes the gap

Put a number in front of any of the commands above and their functionality is multiplied.

 

Other delete commands that cannot add numbers are:

d$ deletes characters from the current cursor until the end of the line

d0 deletes characters from the current cursor until the beginning of the line

J deletes the carriage return character ( CR ) of this line and merges it with the next line.

2.4 , cut and paste

P (lowercase) pastes the contents of the buffer after the current cursor

P (uppercase) pastes the contents of the buffer in front of the current cursor

yy Copy the current line to the cut buffer

nyy copy n lines to cut buffer

 

2.5 , undo and redo

u undo the result of the previous command

(dot) repeats the last command to modify the text

 

3. Insert mode

      3.1

a // Add text to the right of the current cursor position
i // Add text to the left of the current cursor position

A // Add text to the end of the current line

I // Add text to the beginning of the current line ( lines with non-blank characters First )

O // Create a new line above the current line

o // Create a new line below the current line

R // Replace ( overwrite ) the current cursor position and some text that follows

J // Merge the line where the cursor is located and the next line to a line ( still in command mode )

 

      3.2 Body Replacement

ns replaces the n characters after the cursor with the new text

ncw replaces n words after the cursor

ncb replace n words before the cursor

ncd replace the n lines below

 

c$ replaces all characters from the cursor to the end of the line with the new text

c0 replaces all characters from the cursor to the beginning of the line

 

4. Command mode

      All commands start with a colon (:)

      4.1 Exit command
: q // Exit the editor, if the file has been modified, please use the following command

: q! // Exit the editor without saving

: wq // Exit the editor and save the file

      4.2 Use of files

:w // Save the file
:w vpser.net // Save to the vpser.net file

:r file // Read the contents of the file file and place it after the current cursor line

:e file // Edit the new file file to replace the old content

:f file // Change the name of the current text to file

:f // Print the name and status of the current text

:a,bw file // Write the content between line a and line b to the file file

 

      4.3 Line numbers

      All lines of the text being edited have a line number corresponding to them.

:n   moves the cursor to the nth line

Numerical values ​​can be used to indicate absolute line numbers

The period indicates the line number of the line where the current cursor is located

The dollar sign indicates the line number of the last line

Simple numeric expression, as line number

for example:

:.,.+4w myfile // Write the contents of 5 lines from the current line into the myfile file

 

      4.4 String Search

:/string/ advances     the cursor to the next line containing the string string

:?string?    moves the cursor back to the next line containing the string string

:/str1/,/str2/w myfile           will write the text from the line containing str1 to the line containing str2 into the myfile file

 

      Search strings that contain special characters are called regular expressions, regular expressions. Refer to http://cyw.iteye.com/blog/2243996

:/^str/       find lines starting with str string

^         is placed at the beginning of the string, matching the word at the beginning of the line

$         is placed at the end of the string, matching the word at the end of the line

\<       匹配一个字的开头

\>       匹配一个字的结尾

.     (句点)匹配任何单一字符

[str]           匹配str中的各个单一字符

[a-b]          匹配ab之间的任意字符

*              匹配前一个字符的0次或多次出现

\               不管后面字符的特殊含义

 

      4.5正文替换

:s/str1/str2/          str2替换行中首次出现的str1

:s/str1/str2/g        str2替换行中每一个str1

:1,$s/str1/str2/g    str2替换整个文件中的str1

:g/str1/s//str2/g    str2替换整个文件中的str1

g放在命令的末尾表示在当前光标行中对搜索字符串的每一次出现重复使用本命令;g放在命令的开头规定命令对文件中所有包含搜索字符串的行进行替换操作。

 

      4.6删除正文

:d        删除当前光标行

:.,$d

 

5、设置vi的内部变量

:set option

autoindent          自动缩进

ignorecase     不区分规则表达式的大小写,noignorecase关闭本选项

number          行号

ruler               标尺。屏幕底行显示光标行的行号以及在行中的位置。noruler关闭本选项。

tabstop          设置tab跳过的空格数。

 

6shell切换

在编辑环境中运行Linux命令。

:! Command          执行command命令后回到编辑界面



 

 

另外可以参考文章:http://www.vpser.net/manage/vi.html

Guess you like

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