【Study Notes】Vi operation memo

Vi operation memo
      Vi is a screen editing program in the Linux system, which is 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 returned to the original file.
      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 operation modes, namely edit mode, insert mode and command mode.
2.
Edit mode The main purpose of edit mode is to move the cursor position in the edited file.
2.1
k Move up j Move down
h Move left l Move right
ctrl+f Move forward one page in the file
ctrl+b Move backward one page in the file
H Move the cursor to the start line on the screen
M Move the cursor to the screen The middle
L moves the cursor to the last line of the screen
. You can also add numbers 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 Move the cursor right to the beginning of the next line
e Move the cursor right to the end of a word
b Move the cursor left to the beginning of the previous word
0 (zero) Move the cursor left to the beginning of the line
^ Move the cursor to the first non-blank character in the navigation
$ Move the cursor right to the end of the line
2.2. Search string
/string Search forward for the given string string
? string searches back the given string string
n searches 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. Replace and delete
rc Replace the character x indicated by the current cursor with c
Delete the character at the current cursor position
dw Delete the word
db to the right of the cursor Delete the word
dd before the cursor Delete the line where the cursor is located and remove the space
Before any command above Add numbers and their functionality is multiplied.

Other delete commands that cannot add numbers are:
d$ Delete characters from the current cursor until the end of the line
d0 Delete characters from the current cursor until the beginning of the line
J Delete the carriage return character (CR) of this line and merge it with the next line.
2.4. Cut and paste
P (lowercase) Paste the contents of the buffer behind the current cursor
P (uppercase) Paste the contents of the buffer in front of the current cursor
yy Copy the current line to the cut buffer
nyy Add n lines Copy to Cut Buffer

2.5, Undo and Repeat
u Undo the result of the previous command
. (dot) Repeat the last command that modifies 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 Text replace
ns Replace the n characters after the cursor with the new text ncw Replace the n words after the cursor ncb Replace the n words before the cursor
ncd Replace the following n lines c$ Replace with the new text starting from the cursor All characters to the end of the line c0 Replace all characters from the cursor to the beginning of the line 4. All commands in command mode       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 File usage :w //Save the file :w vpser.net //Save to the vpser.net file















:r file //Read in the content of the file file and put it after the current cursor line
:e file //Edit the new file file instead of 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 lines a and b to the file file

      4.3 Line Numbers
      All lines of the text being edited have a line number corresponding to them.
:n Move the cursor to the nth line Numerical value
can be used to indicate the absolute line number.
Period indicates the line number of the current cursor line. The
dollar sign indicates the line number of the last line.
Simple numerical expression, as the line number. For
example :
:.,.+4w myfile //Write the contents of 5 lines into myfile from the current line

      4.4 String search
: /string/ Move the cursor forward to the next line containing the string string
:?string? Move the cursor backward to the next A line containing string
: /str1/,/str2/w myfile will write the text from the line containing str1 to the line containing str2 to

      the search string containing special characters in the myfile file, called regular expressions, regular expressions expression. Refer to http://cyw.iteye.com/blog/2243996
:/^str/ to find out the lines starting with the str string
^ is placed at the beginning of the string, matches the word at the beginning of the line
$ is placed at the end of the string, matches the word at the end of the line
\< matches the beginning of a word
\> matches the end of a word
. (period) matches any single character
[str] matches Each single character in str
[ab] matches any character between a and b
* matches 0 or more occurrences of the previous character
\ regardless of the special meaning of the following characters

      4.5 Body replacement
: s/str1/str2/ Replace with str2 First occurrence of str1 in line
:s/str1/str2/g Replace every str1 in line with str2
:1,$s/str1/str2/g Replace str1 in whole file with str2
:g/str1/s//str2 /g replace str1 in the entire file with str2
g at the end of the command means to repeat the command for each occurrence of the search string in the current cursor line; g at the beginning of the command specifies that the command will search for all characters in the file containing the search character The row of the string is replaced.

      4.6 Delete the text
: d Delete the current cursor line
: ., $d

5. Set the internal variable of vi
: set option
autoindent to automatically indent
ignorecase does not distinguish the case of regular expressions, noignorecase closes this option
number line number
ruler ruler. The bottom line of the screen displays the line number of the cursor line and its position within the line. noruler disables this option.
tabstop Sets the number of spaces tab skips.

6. Shell switching Run Linux commands
in the editing environment.
:! Command Return to the editing interface after executing the command command




. You can also refer to the article: http://www.vpser.net/manage/vi.html

Guess you like

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