Document directory
- 1.1 VI Working Mode
- 1.2 edit mode
- 1.3 insert mode
- 1.4 command mode
- 1.5 option settings
- 1.6 shell Switch
- 1.7 advanced features of vim and gvim
VI Working Mode
Edit mode
Insert mode
Command mode
Shell Switch
Option settings
Advanced features of vim and gvim
1.1 VI Working Mode
After the initial startup, VI enters the editing mode. You can use predefined buttons to move the cursor, delete text, copy or paste text. These buttons are common characters. For example, if l is to move the cursor to the right, K is to move the cursor down, and K is to move the cursor down. In editing mode, you can also use some special buttons to select text and then delete or copy the text.
After you enter commands such as I, A, and O in editing mode, you can enter the insert mode. Type: Enter the naming mode. In insert mode, any character other than ESC entered by the user will be considered as a character inserted into the editing buffer. Press ESC to switch from insert mode to edit mode.
In command mode, VI moves the cursor to the bottom of the screen and displays a colon at the first character ). In this case, you can enter some commands. These commands can be used to save files, Read File Content, execute shell commands, set VI parameters, find strings or replace strings in a regular expression.
1.2 edit mode
1. move the cursor
To modify the body content, you must first move the cursor to the specified position. The simplest way to move the cursor is to press the top, bottom, left, and right arrow keys on the keyboard. In addition to this primitive method, you can also use the many character combination keys provided by VI to move the cursor in the body and quickly reach the specified row or column for positioning. For example, K, J, H, and l functions are equivalent to the top, bottom, left, and right arrow keys.
CTRL + B Move up a page in the file (equivalent to the Pageup key)
CTRL + F move down a page in the file (equivalent to the Pagedown key)
H move the cursor to the top of the screen (highest)
NH moves the cursor to line N of the screen
2 h move the cursor to the second line of the screen
M move the cursor to the middle of the screen (middle)
L move the cursor down the screen (lowest)
NL moves the cursor to the nth line of the screen
3l move the cursor to the last 3rd rows of the screen
W shifts the cursor right in the specified row to the beginning of the next word.
E move the cursor right in the specified row to the end of a word
B. move the cursor left in the specified row to the beginning of the previous word.
0: 0. move the cursor left to the beginning of the line.
$ Move the cursor right to the end of the row
^ Move the cursor to the first non-empty character in the row
2. Replace and delete
After the light is calibrated to a specified position in the file, you can use other characters to replace the characters pointed to by the cursor, or delete one or more characters from the current cursor position. For example:
RC uses C to replace the current character pointed to by the cursor
NRC uses C to replace the first n characters pointed to by the cursor
5rc replaces the first five characters pointed to by the cursor with C
X deletes the current character pointed to by the cursor
NX deletes the first n characters pointed to by the cursor
3X Delete the first 3 characters pointed to by the cursor
DW Delete the word on the right of the cursor
Ndw deletes n words on the right of the cursor
3 DWS Delete the three words on the right of the cursor
DB deletes words on the left of the cursor
NDB deletes n words on the left of the cursor
Delete 5 words on the left of the cursor in 5db
Dd deletes the row where the cursor is located and removes gaps
NDD deletes n rows of content and removes gaps
3 dd delete 3 rows and remove gaps
3. paste and copy
The content (such as characters, words, or rows) deleted from the body is not really lost, but is cut and copied to a memory buffer. You can paste it to a specified position in the body. The command to complete this operation is:
P: lowercase letter P. paste the content of the buffer to the end of the cursor.
P capital letter P, paste the content of the buffer to the front of the cursor
If the buffer content is a character or word, it is directly pasted before or after the cursor; if the buffer content is the whole line of body, paste it to the previous or next row of the row where the current cursor is located.
Note the uppercase and lowercase letters in the preceding two commands. The VI editor often provides a pair of similar functions with a large and lowercase letter (such as P and P. Generally, lower-case commands operate after the cursor, and upper-case commands operate before the cursor.
Sometimes you need to copy a piece of text to a new location, while retaining the content of the original location. In this case, you should first copy the specified content (instead
Cut) to the memory buffer. The command to complete this operation is:
YY copies the current row to the memory buffer
Nyy copies n lines of content to the memory buffer.
Copy 5 lines of content to the memory buffer in 5yy
4. Search for strings
Like many advanced editors, VI provides powerful string search functions. To find the location where a specified word or phrase appears in a file, you can use VI to search directly, instead of manually. The search method is: Enter the character/, followed by the string to be searched, and then press Enter. The editor executes a forward search (toward the end of the file), finds the specified string, stops the cursor at the beginning of the string, and type N to continue the search, locate the next occurrence of the string. Use characters? Replace/to implement reverse search (toward the beginning of the file ). Example:/str1 forward search string str1
N continue searching to find the position where the str1 string will appear next time
? Str2 reverse search string str2
Regardless of the search direction, when the end or the beginning of the file is reached, the search will loop to the other end of the file and continue to execute.
5. Revocation and repetition
When editing a document, you can use the undo command to eliminate the consequences of an incorrect editing command. In addition, if you want
The cursor position of repeat the previously executed edit command. You can use the Repeat command.
U undo the result of the previous command
. Repeat the last command to modify the body.
6. Select text
Vi can enter a visual mode. In this mode, you can use the cursor to move the command to select text visually and then perform other editing operations, such as deleting and copying. V character selected command v line selected command
# DEMO #
1.3 insert mode
1. Enter the insert mode
After you locate the cursor correctly in edit mode, you can switch to insert mode with the following command:
I enter the body on the left of the cursor
A. Enter the text on the right of the cursor.
O Add a new line to the next row of the cursor
O Add a new line to the row where the cursor is located
I enter the body at the beginning of the row where the cursor is located
A. Enter the body at the end of the row where the cursor is located.
The preceding describes several simple methods to switch to the insert mode. There are also some commands that allow you to delete a segment before entering the insert mode
Text to replace the text. These commands include:
S. Replace the character pointed to by the cursor with the input body.
NS replaces n characters on the right of the cursor with the input body
CW replaces the word on the right of the cursor with the input body
NCW replaces n words on the right of the cursor with the input body
CB replaces the word on the left of the cursor with the input body
NCB replaces n words on the left of the cursor with the input body
CD replaces the row of the cursor with the input body
NCD replaces n rows under the cursor with the input body
C $ replace all characters starting from the cursor to the end of the line with the input body
C0 replaces all characters starting from the beginning of the line with the input body with the cursor
2. Exit the insert mode.
To exit the insert mode, Press ESC or Ctrl + [.
# DEMO #
1.4 command mode
In the command mode of Vi, you can use complex commands. In edit mode, type ":". Then, the cursor jumps to the last line of the screen and a colon is displayed. The command mode is displayed. Command mode, also known as "last line mode", displays user input in the last line of the screen, press the Enter key, VI to execute the command.
1. Exit the command
In editing mode, you can use the ZZ command to exit the VI editing program. This command saves the changes made to the body and overwrites the original file. If you only need
Exit the editing program without saving the edited content. Run the following command to exit VI:
Zz
: WQ: X Archive before leaving
: W files are saved as other files
: Q exit (a prompt will be prompted if the file is changed)
: Q! Abandon editing and leave directly
: WQ! Force archiving and exit
Leave Vi temporarily
:! Temporarily execute Unix Commands
: Sh to generate a new shell, CTRL + D back to the original editing location
CTRL + Z return to the previous shell and use the FG command to return to VI
2. line number and file
Each line in the editing contains its own line number. You can use the following command to move the cursor to the specified line:
: N move the cursor to line N
In command mode, you can specify the range of line numbers for command operations. A numeric value is used to specify an absolute line number. The character "." indicates the line number of the row where the cursor is located.
The symbol "___ fckpd ___ 9 rdquo; indicates the row number of the last line of the body. A simple expression, such as". + 5 ", indicates the second row down the current row. For example:
: 345 move the cursor to line 345th
: 345 million file write 345th rows into the file File
: 3rd W file: Write lines 5th to file files
: 1,. W file writes row 1st to the current row into the file File
:., $ W file: Write the current row to the last row to the file File
:... + 5 W file: write 6 lines of content into the file from the current row
: 1, $ W file writes all content to the file, equivalent to the w file command
In command mode, you can read or write the body from a file. For example:
: W writes the edited content to the original file to save the intermediate editing result.
: WQ writes the edited content to the original file and exits the editing program (equivalent to the ZZ command)
: W file writes the edited content to the file, keeping the content of the original file unchanged.
: A, BW file writes the content from row A to row B to the file File
: R file reads the content of the file and inserts the content behind the row where the current cursor is located.
: E file: edit the new file to replace the original content.
: F file: Rename the current file to file
: F print the name and status of the current file, such as the number of lines of the file and the number of lines where the cursor is located.
3. string SEARCH
Returns a string that can be searched to reach the specified row. If you want to perform a forward search, place the string to be searched in two
/"; If you want reverse search, place the string in Two"?" . For example:
:/Str/forward search, move the cursor to the next row containing the string Str
:? Str? Reverse search: move the cursor to the previous row containing the string Str
:/Str/W file: Forward search, and write the first row containing the string STR to the file.
:/Str1/,/str2/W file forward search, and write the line containing the string str1 to the line containing the string str2 into the file File
4. Text replacement
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/g functions are the same as above
From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search
String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.
5. Delete the body
In command mode, you can also delete content in the body. For example:
: D. Delete the row where the cursor is located.
: 3D delete 3 rows
:., $ D Delete the current row to the end of the body
:/Str1/,/str2/d delete all rows from string str1 to str2
6. Restore Files
VI will generate another temporary file when editing a file. The file name usually starts with. And ends with. SWP. VI in
When the file Exits normally, it is deleted. If the file unexpectedly exits without saving the latest modification content, you can use the recovery command:
: Recover recovery file
You can also use the-r option when starting VI.
# DEMO #
1.5 option settings
To control different editing functions, VI provides many internal options. Use the SET command to set options. The basic syntax is:
: Set option set Option
Common features include:
Autoindent: sets this option, and the body is automatically indented.
If this option is set for ignorecase, the differences between upper and lower case letters in the Rule expression are ignored.
Number.
Ruler sets this option to display the position of the row and column of the cursor at the bottom of the screen.
Tabstop sets the number of spaces skipped by pressing the tab key. For example, set tabstop = n. The default value of N is 8.
MK saves the options in the. exrc file of the current directory.
1.6 shell Switch
When editing the body, use the shell switch Command provided in VI command mode to execute the Linux Command without exiting VI.
Convenience. Syntax format:
:! After executing the shell command, return to VI
In edit mode, Enter K to run the VI command to find the manual page of the word where the cursor is located. This is equivalent to running the man command. # DEMO #
1.7 advanced features of vim and gvim
Vim stands for VI improved. As its name implies, VIM exists as an upgraded version of the standard UNIX system VI editor. Besides providing the same powerful functions as the VI Editor, VIM also provides multi-level recovery, command line history, command and file name completion functions.
Gvim is the X Window version of vi. It supports mouse selection and some advanced cursor movement functions with menus and tool buttons.
========================================================== ======================================
Insert/command mode switch
ESC key insertion mode --> command mode
Commands in command mode
A. Insert text to the right of the cursor
I insert text at the cursor
O Add a new column under the cursor
A inserts text at the end of the column at the cursor
I insert text at the beginning of the cursor Column
O Add a new column above the cursor
Move cursor
K move up
J move down
H move to left
L move to the right
^ Move to the beginning of a column
$ Move to the end of a column
W shifts one word to the right (word)
B shifts one word to the left)
E move to the last character of the current word
G move to tail
Ng moved to column N
: N move the cursor to column N
: $ Move the cursor to the last column
H move the cursor to the top of the screen
M move the cursor to the center of the screen
L move the cursor to the bottom of the screen
CTRL + D move cursor down half page
CTRL + u cursor to the top half page
CTRL + F cursor to roll a page
CTRL + B cursor to the next page
CTRL + G query the current cursor position
Delete data
X Delete the character of the cursor)
# X start with the cursor and delete # characters
X deletes the first character (backspace) of the cursor)
# X start from the cursor and delete # characters forward
DW deletes a word)
# DW Delete # words (word)
D. Delete the word to the end of the column.
Delete the entire dd Column
# Dd start from the cursor column and delete # column
DG deletes from the cursor to the end of the file
Moving and copying
The NDD starts n columns from the cursor and goes to the temporary storage area.
Nyy starts n columns from the cursor and copies the temporary storage area.
P paste the content of the temporary storage area to the cursor
N, m Co x copies data from N to M columns to column X
N, m x move data from N to M columns to X Columns
., $ Co x: copy the data from the cursor column to the end of the file to column X.
Search and replace
/String SEARCH
: Case Insensitive for set IC search
: Case sensitivity is required for set noic search.
N downward search
N search up
R: Replace the cursor with another character.
CW replaces a string
CC replaces the entire column
: S replaces multiple strings
: 1, $ S/old string/New String/G 1 is the 1st column, $ is the last column, and G is the all-round
For example, you can change the string of all A: B:... z: In the file *
: 1, $ S/[A-Z]:/*/g
Merge columns/Files
J. merge two columns
: R file name inserts the file into the column where the cursor is located
: N r partition name inserts the file after column N
U cancel the previous command (UNDO)
U cancels all modifications to this column
: Set nu plus row number
: Set Nonu does not display row numbers
---------------------------------------------------------
Access the VI command
VI filename: open or create a file and place the cursor at the beginning of the first line
Vi + N filename: open the file and place the cursor at the beginning of line N
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi +/pattern filename: open the file and place the cursor at the first string matching pattern
VI-r filename: a system crash occurred when VI was being edited last time, restoring filename
VI filename... filename: open multiple files and edit them in sequence
Move cursor command
H: move the cursor one character to the left
L: move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl + P: move the cursor up a row
J or Ctrl + N: move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move the cursor right to the end of a word J.
): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line N
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line N
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
Screen tumble command
CTRL + u: half screen at the beginning of the file
CTRL + D: Flip the half screen to the end of the file
CTRL + F: Flip the screen to the end of the file
CTRL + B; open a screen at the beginning of the file
NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.
Insert text commands
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text
NCW or NCW: modify a specified number of characters
NCC: modifies a specified number of rows.
DELETE command
Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
NDD: Delete the current row and the next n-1 row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
CTRL + u: delete text entered in input mode
Search and replace commands:
/Pattern: Search for pattern from the beginning of the cursor to the end of the file
? Pattern: Search for pattern from the beginning of the cursor
N: Repeat the previous search command in the same direction.
N: Repeat the previous search command in the reverse direction.
: S/P1/P2/G: replace all p1 in the current row with P2.
: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.
: G/P1/S // P2/G: replace all P1 files with P2.
Option settings
ALL: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (CTRL + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: No write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "/" in search mode.
Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.
Mesg: Allows VI to display the information that other users write to their terminal using write.
Last-line command
: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit VI.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed.
If N1, N2 is specified, the entire file content is used as the command input.
: R! Command: place the output result of the command to the current line.
Access the VI command
VI filename: open or create a file and place the cursor at the beginning of the first line
Vi + N filename: open the file and place the cursor at the beginning of line N
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi +/pattern filename: open the file and place the cursor at the first string matching pattern
VI-r filename: a system crash occurred when VI was being edited last time, restoring filename
VI filename... filename: open multiple files and edit them in sequence
Move cursor command
H: move the cursor one character to the left
L: move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl + P: move the cursor up a row
J or Ctrl + N: move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move the cursor right to the end of a word J.
): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line N
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line N
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
Screen tumble command
CTRL + u: half screen at the beginning of the file
CTRL + D: Flip the half screen to the end of the file
CTRL + F: Flip the screen to the end of the file
CTRL + B; open a screen at the beginning of the file
NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.
Insert text commands
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text
NCW or NCW: modify a specified number of characters
NCC: modifies a specified number of rows.
DELETE command
Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
NDD: Delete the current row and the next n-1 row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
CTRL + u: delete text entered in input mode
Search and replace commands:
/Pattern: Search for pattern from the beginning of the cursor to the end of the file
? Pattern: Search for pattern from the beginning of the cursor
N: Repeat the previous search command in the same direction.
N: Repeat the previous search command in the reverse direction.
: S/P1/P2/G: replace all p1 in the current row with P2.
: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.
: G/P1/S // P2/G: replace all P1 files with P2.
Option settings
ALL: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (CTRL + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: No write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "//" in search mode.
Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.
Mesg: Allows VI to display the information that other users write to their terminal using write.
Last-line command
: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit VI.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed.
If N1, N2 is specified, the entire file content is used as the command input.
: R! Command: place the output result of the command to the current line.
Input mode command
A. Enter text after the cursor
A. Enter text at the end of the current row.
I input text before the cursor
I. Enter text in the current line.
O enter a new row after the current row
O enter a new row before the current row
Cursor movement command
B. Move to the start of the current word.
E. Move to the end of the current word.
W move a word forward
H move one character forward
J. Move one row up
K move a row down
L move one character backward
Delete Operation Command
X Delete the character of the cursor
DW deletes the word with the cursor
D $ Delete All characters from the cursor to the end of the line
D is the same as d $
Dd deletes the current row
You can add a number before the DELETE command. For example, 5x indicates that five rows are deleted.
Change and replace operation commands
R replaces the character of the cursor
R replacement Character Sequence
CW replaces a word.
Ce and CW
CB replaces the previous character of the cursor
C $ replace all characters from the cursor position to the end of the line
C is the same as C $
CC replaces the current row
Query command
Command function
/ABC forward query ABC
? ABC backward query ABC
N forward query
N continue Query
Copy and paste commands
Command function
/YW uploads the word with the cursor to the clipboard
Y $ move the cursor to the end of the line into the clipboard
Y is the same as Y $
YY uploads the current row to the clipboard
P paste the content in the clipboard after the cursor
P paste the content in the clipboard before the cursor
File Save and exit VI command
Command function
: Q does not store packets and exits.
: Q! Do not save mandatory exit
: W save and edit
: W filename is stored in file filename
: W! Filename is forcibly stored in filename.
: WQ save and exit
: X same as: WQ
ZZ: WQ