Explanation of Linux commands vi interactive text editor
Vi is short for 'visual interface'. It is a standard interactive text editor on the character-only interface in Linux. You can perform output, delete, search, replace, block operations, and many other text operations. You can also customize them as needed. All of its operations must be completed by using keyboard commands. If you are familiar with the application, it will be very convenient.
--------------------------------------------------------------------
Working Mode:
Vi has three working modes: Command mode, Insert mode, and Last line mode.
Command mode: Command mode. (Default mode)
When you enter the vi Editor, this mode is used by default.
In this mode, you can control the movement of the screen cursor, copy, paste, and delete characters or rows.
The input commands in this mode are invisible.
Insert mode: Insert mode.
You can only enter text in this mode.
In this mode, add or delete the modified text content.
Last line mode: Last line mode. (Special Command mode)
In this mode, you must press enter to perform the operation.
In this mode, you can save the file and exit vi.
In this mode, you can set environment variables.
In this mode, you can find and replace
This mode automatically returns to the command mode after the execution is complete.
Vi can be simplified into two modes: Command and Insert mode. Last line is a special Command mode.
--------------------------------------------------------------------
Go to vi:
1: # vi filename: open (or create) the file and place the cursor in the first line
2: # vi + n filename: open the file and place the cursor in line n.
3: # vi + filename: open the file and place the cursor in the last line
4: # vi +/word1 filename: open the file and place the cursor in the first word1
5: # vi-r filename: Restore filename (if the system crashes when vi is edited last time)
6: # vi filename1 filename2 filename3: open multiple files and edit them in sequence
Ps:
1: If filename exists, open filename. If not, create a new filename)
2: The blank line is shown as a Tilde symbol before each row.
3: If you do not save changes to the file, the content of the original file will not be changed.
4: multiple users can use vi to edit an object at the same time. The last saved version will be saved.
--------------------------------------------------------------------
Three modes:
Command mode to insert mode: 'I ''a' C' o ''r'', etc.
Insert mode to command mode: 'esc'
Command mode to last line mode :':''/''? '
Last line mode to command mode: 'esc'
The insert mode to the last line must be switched to the command mode.
--------------------------------------------------------------------
Help:
F1
: Help
View version:
# Vi -- version
--------------------------------------------------------------------
Screen flip:
Ctrl + f: Move the screen to the front page (commonly used) Ctrl + B: Move the screen to the back page (commonly used) Ctrl + d: move the screen to the front half page Ctrl + u: Move the screen to the back page Ctrl + e: Roll the screen forward line Ctrl + y: Roll the screen item back line
--------------------------------------------------------------------
Move the cursor:
Ctrl + G: displays the number of rows and columns where the cursor is located. 0: move the cursor to the beginning of the current row (number 0) ^: move the cursor to the first non-blank character of the row $: move the cursor to the end of the current line (commonly used) n $: move the cursor to the end of line n (: to the beginning of the sentence): To the end of the sentence {: to the beginning of the paragraph }: to the end of the paragraph G: move the cursor to the last line of the article (commonly used) nG: move the cursor to the first line of n gg: Move the beginning of the article h or to the left direction key: move the cursor to the left one character l or right direction key: move the cursor to the right one character k or up direction key: move the cursor up one character j or down direction key: move the cursor down one character Ctrl + p: move the cursor up one line (same k) Ctrl + n: move the cursor down one line (same as j) Enter: move the cursor down one line n <Enter>: move the cursor down n lines space: move the cursor one character to the right n <space>: move the cursor to the right n characters Backspace: move the cursor one character to the left n <Backspace>: move the cursor to the left n characters B: move the cursor left to the beginning of the previous word B: move the cursor left to the beginning of the word separated by the previous space w: move the cursor right to the beginning of the next word W: move the cursor right to the beginning of the word separated by the next space e: move the cursor right to the end of the next word E: move the cursor right to the end of the word separated by the next space H: move the cursor to the top line of the current screen nH: move the cursor to the nth line of the screen L: When the cursor moves to the last line of the current screen more than one screen, when the cursor moves to the bottom line of the screen with less than one screen, the cursor moves to the last line of the file nL: The cursor moves to the last n rows of the screen M: when the cursor moves to more than one screen in the middle line of the current screen, when the cursor moves to the middle line of the screen content less than one screen, the cursor moves to the middle line of the text dH: delete all content from the row where the cursor is located to the first line of the screen dM: delete all content from the row where the cursor is located to the middle line of the screen (File) dL: delete all content from the row where the cursor is located to the last line of the screen +: move the cursor to the first non-blank character n + in the next line +: move the cursor down n rows -: move the cursor to the first non-blank character n-: move the cursor up n rows z.: use the current row as the middle line of the screen v: select text content
--------------------------------------------------------------------
Insert text
A: insert character A after the current cursor: insert character I after the end of the current row: insert character I before the current cursor: insert a character before the beginning of the current line (before the first non-blank character) o: insert an empty line after the current line, start from the beginning of the next line at the cursor input character O: insert an empty row in front of the current row, and enter the character r from the beginning of the previous row where the cursor is located: Replace the character of the current cursor (after replacement, it ends) R: replace the current and subsequent characters until you Press ESC to end with J: append the next line of the row where the cursor is located to the end of the row: r filename: add the content in filename to the next row of the current cursor: r! Command adds the command shell command output result to the current line xp: swap the characters at the cursor position and the characters on its right ~ : Change the case sensitivity of the output character of the cursor.
--------------------------------------------------------------------
Delete:
Dd: Delete the row where the cursor is located. ndd: delete n rows starting from the row where the cursor is located. D: Delete the content after the cursor of the row where the cursor is located. x: Delete the character X where the cursor is located: delete the character nx before the cursor: Delete the n characters nX starting from the cursor position: Delete the n characters dw before the cursor: delete the word where the cursor is located (actually starting from the cursor position to the end of the word) (including spaces) de: delete all characters from the cursor to the end of the word (excluding spaces) d0: delete all content before the row cursor where the cursor is located d ^: delete all characters from the current character to the beginning of the line (same as d0) d $: delete all content d1G from the row where the cursor is located to the end of the row: delete all data from the row where the cursor is located to the first row dG: delete all data from the row where the cursor is located to the last row dfa: delete content from the cursor to the first a character (including a): nd: Delete row n: n1, n2d: Delete content between row n1 and row n2 s: delete the character at the cursor position, append any character S: Delete the current row, append any character nS: delete n rows, and append any character cw: delete the character ce from the cursor to the end of the word: Delete the character from the cursor to the end of the word (same as cw) c $: Delete the character C from the cursor to the end of the current row: delete the character from the cursor to the end of the current row (same as c $) c ^: Delete the character from the cursor to the beginning of the line (excluding the character on the cursor) cc: delete the row where the cursor is located (different from dd: cc enters the insert state) cb: Delete the character before the cursor of the word where the cursor is located
--------------------------------------------------------------------
Undo and repeat
U: undo the previous operation (can be executed multiple times) U: undo all operations on the current row.: Repeat the previous operation Ctrl + R: Restore the Undo operation
--------------------------------------------------------------------
Search:
/Word: Search for word from the cursor (if the end of the file is reached, start from the beginning )? Word: Search for word from the cursor (if a file header is encountered, start from the end) n: Execute the last defined search in the Same Direction N: Execute the last defined search in the opposite direction /: repeat the last defined search: set ic: Case Insensitive when searching: set noic: Search Time Zone case sensitive
--------------------------------------------------------------------
Replace:
: S/word1/word2/c replace word1 with word2, and ask the user to confirm before replacement: s/word1/word2/g replace all word1 in the current row with word2: n1, n2s/word1/word2/g replace all word1 from row n1 to row n2 with word2: 1, $ s/word1/word2/g replace all word1 from the first row to the last row with word2: g/word1/s // word2/g replace all word1 in the file with word2: % s/word1/word2/g replace all word1 in the file with word2: n1, n2s/^/word1/g and add word1 to the beginning of line n1 to line n2: % s/* $ // Delete spaces at the end of all rows (super common): % s #/dodo/1 #/dodo/2 replace the Separator
Without g, replace the first appearance of the search string
G at the end of the command, replace each appearance of the row where the search string is located
G. At the beginning of the command, replace all the lines in the text.
--------------------------------------------------------------------
Save and exit:
: W: Save the current file: q: exit directly (prerequisite for no modification to the file): wq: Save and exit: x: Save and exit: q! : Exit directly without saving (do not want to save changes to the file): w! : Force save this file (for read-only files) SHIFT + zz: Save and exit (actually ZZ ): w filename: Save the modified file to filename (if filename exists, an error is reported): n1, n2w filename: Save the content from line n1 to line n2 to the filename file.
--------------------------------------------------------------------
Hide the row number:
: Set nu display row number: set nonu cancel display row number Ctrl + G: list the row number of the cursor
--------------------------------------------------------------------
Copy and paste:
Yy: copy the row where the cursor is located (commonly used) nyy: copy from the row where the cursor is located to the next n rows y1G: Copy all rows from the row where the cursor is located to the first row yG: copy all rows from the row where the cursor is located to the last row yw: copy the word from the cursor position to the end of the word y $: copy the content starting from the cursor to the end of the line y ^: copy the content starting from the cursor to the beginning of the line y0: copy the content starting from the cursor to the beginning of the line (same as y ^) p: paste in the next row of the current line (lower case) P: paste in upper case of the current row)
Note: All 'y' copies must be combined with 'P' to complete copying and pasting.
: N1, n2 co n3 convert n1 ~ The content of n2 is copied to line n3. : N1, n2 co $ convert n1 ~ Copy the content of row n2 to the last row. : N1, n2 mo $ change n1 ~ The content of the n2 row is moved to the last row.
Buffer usage (note that the buffer name should not conflict with the instruction name)
Vi has the buffer function. You can store data in the buffer.
Cyy: copy the current row and put it in buffer c. cp: paste the content in buffer.
--------------------------------------------------------------------
Modify file format
: Set fileformat = unix
: Set ff = unix
Change the file to unix format (for example, ^ M will appear in the text file under win in linux, leading to garbled characters)
--------------------------------------------------------------------
Other commands:
:! Command: Execute the shell command Command !! : Re-execute the last (:! Command sub-Command) <: Move the current row to the left a tab>: Move the current row to the right a tabn>: Move a tab from the n rows starting the current row to the right: n1, n2>: Move line n1 to line n2 one tab to the right: n1, n2 >>>: move line n1 to line n2 three tabs to the right.
--------------------------------------------------------------------
Environment variable settings
1. Set through the last row Mode
For example: set autoindent
The last row mode is temporary, and no longer exists after you exit vi.
2. To save these settings permanently, you can save the option settings to the following files so that they are valid for each vi operation:
/Etc/virc or ~ /. Virc
~ /. Exrc or. exrc in the current working directory
Common options are as follows:
Set tabstop = 4 # set the number of spaces skipped by tab to 4 (8 by default) set number # set the display body line number set ignorecase # ignore case sensitivity (ic) in search commands) set noignorecase # differences between Case sensitivity and Case sensitivity in search commands set scroll = 20 # set the number of lines to be rolled by the 'ctrl + d' command in command mode set showmode # set the vi editor mode to be displayed in the right corner of the screen (abbreviation: smd) set RSE # set terse to display the row and column of the cursor at the bottom of the screen # set terse to show only brief warning information set nomagic # set in search mode, special characters without '\' are allowed (Symbols do not have special meanings) syntax on # set the syntax to highlight and display set autoindent # set the same indentation for each row and the previous line (ai) set smartindent # sets that each line has the same indentation as the previous line. However, if '{' and '}' and '#' are met, no indentation is used (Abbreviation: si) set cindent # uses the Standard C language indentation (C, C ++, Java) (Abbreviation: ci) set shiftwidth = 2 # sets the number of indentations to 2 (Abbreviation: sw) set report # inform the user of the row number set wrapmargin = 80 # set the line length to 80 (wm)