CentOS VI Common Commands

1. The mode of vi
Command mode: vi starts the mode entered by default, you can enter the input mode "i", or the last line mode (:), in addition to completing the function of the command, it is also the conversion point of the latter two modes.
Input mode: That is, the text editing state, the last line displays "-- INSERT --" prompt, press ESC to return to the command mode Last
line mode: can complete some complex operations, start with ":", and automatically return to the command after executing a last line command Mode

2. Basic use
1. Start and exit of the vi editor:

(1). Use the command "vi" to directly enter the editing environment
(2). Enter ":q!" in the command mode to forcibly exit the editing environment. The file modification will not be saved
(3). When the file name is used as the parameter of the "vi" command, if the file exists, vi can edit the content. If it does not exist, open the empty file named after the parameter name.

2. File operation :

(1). Open a new file: ":e filename" closes the current file and opens a new file in the last line mode, ":e! filename forces the current file to close and opens a new file"
(2). Read the file content to Current file: ":r filename", paste the read file content below the line where the cursor of the current file is located
(3) Save the file: The command is ":w [filename]", if the file is already named, you can use ":w directly" "The parameter of the command, if it is not named, you need to specify the file name as the parameter when saving for the first time
(4). Save as a file: the command is ":w filename" , only save to a new file without updating the content of the original file
(5). Exit vi editor: exit format is ":q", save and exit as ":wq", force exit as ":q!"

3. Cursor movement operation (in command mode)

(1). Cursor direction movement: "h" left, "l" right, "k" up, "j" down
(2). Page turning movement: "ctrl+f" previous page, "ctrl+b" back One page, the first half page of "ctrl+u", the second half page of "ctrl+d"
(3). Quick jump in line: "^" the first character of this line, "$" the last character of this line, the word after "w" The first letter, the first letter of the word before "b", the last letter of the word after "e"; it can be used in combination with numbers, for example: the first letter of the three words after "3w", the first letter of the first four words of "4b", " 5 words ending letters after 5e"
(4). Jump between lines in the file: display the line number ":set nu", cancel the display of the line number ":set nonu", "1G" jumps to the first line, "G" jumps Go to the last line, "#G" jumps to the #th line

4. Edit operation

(1). Enter input mode: "i" at the current cursor, "a" after the current cursor, "A" at the end of the current line, "o" Insert a new line under the current line, "O" insert a new line on the current line, "cw" delete the character at the end of the current cursor,
   "c$" delete the character from the current cursor to the end of the line, "c^" delete the current cursor (not Including the characters before the ) to the beginning of the line; if the above memory is difficult, just remember "i" to enter the input mode from the command mode, and "Esc" after finishing editing Return to command mode
(2). Operation in input mode: move the cursor in the up, down, left and right directions, "Home/End" positions the cursor to the beginning of the line and the end of the line, "Page Up/Down" turns the page up and down, and the characters on the left of "Backspace" Delete, "Delete" deletes the character at the cursor position
(3). Delete operation: In command mode, the following command can be used to complete the delete operation, "x" a single character at the cursor, "dd" the line where the cursor is located, "dw" the current character to End of word (including spaces), "de" current character to end of word (without spaces),
     "d$" The current character goes to the end of the line, "d^" The current character goes to the beginning of the line, "J" merges the contents of the current line and the next line, and numbers can be added for use, for example, "3x" The current cursor is 3 characters to the right, "2dd" starts two lines of text from the current line,
     "3dw" the current cursor is 3 words to the right, "2de" the current cursor is two words to the right (retain a space), "5J" merges 5 lines of text
(4) .Undo operation: In command mode, "u" cancels the last operation, which can be used repeatedly, "U" cancels all operations on the current line, and "Ctrl+R" restores the undo generated by "u"
(5) .Copy operation: "yy" entire line, "yw" cursor to the end of the word, "y$" cursor to the end of the line, "y^" cursor to the beginning of the line; used in combination with numbers, "5yy" current and subsequent 5 lines , "3yw" 3 words starting from the current cursor;
the last line command ":m,ny" the text between lines m and n, for example: ":100,200y" content from lines 100 to 200
(6). Paste operation : In command mode, use "p" to paste the contents of the buffer into the current

document

, "n" to find the next (top-down), "N" to find the next reverse (bottom-up)
(2). Bottom-up: ":?word" searches for the specified string from bottom to top, " n" to find the next (bottom-up) "N" to find the next reverse (top-to-bottom)
(3). Ordinary replacement: ":s/old/new" starts with s, and "old" means to be replaced The string, which string to replace "new" with, this command only replaces the first matching string in the current line
(4). Replace all in the line: ":s/old/new/g", all matches in the current line String
(5). Replace in the line area: ":#,#s/old/new/g", "#,#" means the line area between two line numbers, for example "3,5" means the third Line to line 5
(6). Replacement operation in the entire file: ":%s/old/new/g", adding % means the entire document, dangerous, use it with caution
(7). Use the replacement confirmation function: replace the original Add "c" after the command to provide confirmation, for example: ":s/old/new/c", ":s/old/new/gc", ":#,#s/old/new/gc", " :%s/old/new/gc";
the prompt after confirmation of the replacement command is "replace with word (y/n/a/q/l/^E/^Y) ?", where "y" is replaced, "n "Abandon, "a" all, "q" to quit

3. Multi-file operation

of the editor 1. Vi startup of multi-file operation: "vi [file1] [file2] .." followed by multiple file names as parameters, The first parameter is displayed on the screen, and other files are in the editing background state
. 2. Multi-file switching command:
(1). Display multi-file information: the ":args" result shows the open file, and the "[]" is the current display File
(2). Switch files before and after: ":next" displays the next file, ":prev" displays the previous file, and "!" is added to force the switch without saving the current file
(3). Locate the first and last files: ":first" locates the first file, ":first" "last" to locate the end file
(4). Quick file switching: After a switching command has been used to switch files, "Ctrl+^" can quickly switch to the previous state.

Move and copy
==========
        Use delete and The put command can accomplish the purpose of data movement.
        The purpose of data copying can be accomplished by using yank and put commands.
        yank and delete can copy the specified data to the memory buffer, and
        the data in the buffer can be copied to the screen by the put command.
        Example:
        move a line. Execute dd on this line
                       . The cursor moves to the destination
                       . Execute p
        to copy a line. Execute yy on this line
                       . The cursor moves to the destination
                       . Execute p
. Instruction repetition
=========
        In the instruction mode, you can add a number n in front of the instruction, then the instruction action will be repeated n
        times.
        Example:
        delete 10 lines. 10dd
        copies 10 lines. 10yy
                               . The cursor moves to the destination
                               . The p
        index moves down 10 lines. 10j
.. Cancel the previous action (Undo)
==================
        That is to restore the content before the execution of the previous command.
       u restores the result before the last instruction.
       U Reverts all changes to the row of the cursor.
..Search
=====
        You can search for a string in vi and move the cursor there.
       /string Find the string after the cursor.
        ?string Finds the string before the cursor.
        n Continue down to find the next identical string.
       N continues upward to find the next identical string.
..Data connection
===========
       J sentence connection. Joins the row below the cursor to the row behind the cursor.
       If a line of data is too long, it can also be divided into two lines, just move the cursor to the split point, enter the input mode
        (you can use commands such as a, i, etc.), and then press "Enter".
.. Environment setting
===========
        : setnu sets the line number of the data.
        :setnonu Cancels line number setting.
        :setai indents automatically.
        :setnoai Cancel automatic indentation.
       Automatic indentation (automatic indentation)
        When editing files or programs, sometimes you may encounter situations that require indentation. ":set ai" provides the function of
        automatic indentation, which is explained with the following example:
        .vi test
        . (After entering the editing window)
          this is the test for auto indent
          《Tab》start indent ← :set ai (set auto indentation)
          《Tab》data
          《Tab》data
          《Tab》data ← :set noai (cancel auto indentation)
        the end of auto indent.
        .Note: <Ctrl><d> deletes the "Tab" character.


Delete from the current position to the end of the line
d$
Copy from the current position to the end of the line
y$ If you want to paste it elsewhere p is fine

13.
:%s/old_word/new_word/g
This command is to replace specific characters in the entire file String
14. Cursor control
k: move up nk move up n lines
j: move down nj move down n lines

move the cursor to the nth line, press mk
to move the cursor to the mth line, press "ay'k
to store the nth to mth lines in the a register, and so on, b, c........ register
,

etc ap, and so on, b, c........ registers, etc.
In the current screen,
H jumps to the first line
M jumps to the middle line
L jumps to the last line
15.
Table 8-2 Delete command
Delete Command operation
dl Delete the current character (same function as the x command)
d 0 Delete to the beginning of a line
d ^ Delete to the first character of a line (excluding spaces or TAB characters)
dw Delete to the beginning of a word End position
d 3 w Delete to the end of the third word
db Delete to the beginning of a word
d W Delete to the end of a space delimited word
d B Delete to a space delimiter d 7 B Delete to the beginning of
the first 7 words separated by spaces
d) Delete to the end of a statement
d 4) Delete to the end of the fourth statement
d ( Delete to a the beginning of a statement
d } delete to the end of a paragraph
d { delete to the beginning of a paragraph
d 7 { delete to the 7th paragraph before the beginning of the current paragraph
dd delete the current line
d /text delete from the text where the word specified in "text" appears, all the way forward up to the next occurrence
of (but not including the word)
d fc delete the occurrence of the character "c" from the text, forward until the next occurrence of the word (including
the character)
d tc delete the current line until the next occurrence of the character "c"
D delete to the end of a line
d $ delete to the end of a line
5 dd delete from the beginning of the current line 5 lines of content
d L Delete content up to the last line on the screen
d H Delete content up to the first line on the screen
d G Delete content up to the end of the working buffer
d 1 G Delete content up to the beginning of the working buffer

Replace command
Replace command operation
s replaces the current character with one or more characters
S replaces the current line with one or more characters
5 s replaces 5 characters from the current character with one or more characters

vi replaces using rules:
:g/s1/ s/s2/s3/g
The first g means to replace every line including s1, the second g means to replace all s2 with s3 for every line including s1
s means replacement, s2 is the string to be replaced, it can be the same as s1 (if it is the same, use // instead), s3 is the replacement string

Guess you like

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