1. vim # enter vim in the command line and press the I key in the vim editor 2. I #. -- INSERT -- # INSERT command is displayed at the lower end, any character in vim may be used. 3.Esc# exit the I (insert) command for other commands. 4.: rfilename # read the content of a file and write it to the current editor.: wnewfilename # Replace
1.
Vim
# Enter vim in the command line to enter the vim editor
2.
I
# Press the I key to display -- INSERT --
# Insert command, which may be useful for any character in vim
3.
Esc
# Exit the I (insert) command for other commands
4.
: R filename
# Read the content of a file and write it to the current editor.
5.
: W newfilename
# Write the content in the editor to a new file
6.
: W
# Save the file during editing, which is equivalent to ctrl + s in word.
7.
:! Command
The result of executing command in command column mode is displayed when vi is temporarily removed! For example
:! Ls
# Execute the shell command ls during editing
8.
: Sh
# Enter the shell command line. after the command is executed, press ctrl + d to exit and re-enter vim edit to continue editing.
Run the shell command ctral + l to clear the screen.
9.
: Wq
# Save the file and exit
10.
ZZ
# Save the file and exit. use the same command. Make sure to use uppercase letters.
11.
: Q!
# Force exit, not save
12.
: Set number or: set nu
# Display the row number of the edited file
13.
: Set nonumber or: set nonu
# Opposite to the previous command, no line number is displayed
14.
: Help I
# View the help of the insert command
15.
U
# Undo the previous operation
16.
/Fedora
# Find Fedora characters
17.
: S/Fedora/Redhat
# Replace Fedora with Redhat (only in the row where the cursor is located)
18.
Dw
# Deleting words
Dd
# Deleting rows
19.
O
# Open a blank line
20.
Vim + filename
# Edit the last line of the file
21.
Vim + n filename
# Enter row n of the file for editing
22.
: 1,. s/redhat/fedora
#. Number indicates the current row, that is, the row where the cursor is located
# Replace the redhat character that appears for the first time from 1st rows to the current row (.) with fedora
23.
: 1,. s/redhat/fedora/g
# Replace all the redhat characters that appear from the first row to the current row (.) with fedora, g
Global flag
24.
: 1, $ s/redhat/fedora/g
# $ Indicates the last row
# Replace all the redhat characters from the first row to the last row with fedora
25.
: % S/redhat/fedora/g
# Same command
26.
: % S/\/fedora/g
# Replace all the redhat characters from the first row to the last row with fedora
# Words, not characters
27.
: F
# Display file content and status
# Same as ctrl + g commands
28.
: E!
# The current file is returned to the last saved file.
: E file
# Switching and editing files
29.
: N
# When editing multiple files (such as vim file1 file2), switch to the next file.
Combined use
VIM commands
Cursor Control Command
Command cursor movement
H shifts one character to the left
J: move down a row
K move up a row
L move one character to the right
G move to the last row of the file
W moves to the beginning of the next word
W moves to the beginning of the next word, ignoring punctuation
B is moved to the beginning of the previous word
B moves to the beginning of the previous word, ignoring punctuation marks
L move to the last line of the screen
M moves to the middle line of the screen
H move to the first line of the screen
E move to the end of the next word
E. move to the end of the next word and ignore punctuation marks.
(Move to the beginning of a sentence
) Move to the end of the sentence
{Move to the beginning of a paragraph
} Move to the beginning of the next paragraph
0 (number), | move to the first column of the current row
^ Move the first non-null character to the current row
$ Move to the last character of the current row
+, Enter moves to the first character of the next line
-Move to the first non-null character in the previous line
Add text in vi
Command insert action
A insert text behind the cursor
A inserts text into the current row
I insert text before the cursor
I insert text before the current row
O insert a new row under the current row
O insert a new row above the current row
S. delete the character at the cursor and enter the insert mode.
S. delete the row where the cursor is located and enter the insert mode.
: R file reads the file content and inserts it into the current row.
: Nr file reads the file content and inserts it after line n
Esc back to command mode
^ V char indicates that the specified meaning of char is ignored during insertion to insert special characters.
Delete text in vi
Command to delete
X delete characters at the cursor
Delete dw to the beginning of the next word
DG deletes the row until the end of the file
Dd deletes the entire row
Db deletes the word before the cursor
: N, md deletes n rows from row m.
D, d $ delete from cursor to end of line
^ H: when backspace is inserted, the previous characters are deleted.
^ W delete the previous word when inserting
Modify vi text
The number before each command indicates the number of times the command is repeated.
Command replacement operation
Rchar replaces the current character with char
R text escape replaces the current character with text until the Esc key is pressed
Stext escape uses text to replace the current character
S or cctext escape replace the entire line with text
Cwtext escape: change the current word to text
Ctext escape: change the remaining text of the current row to text
Change cG escape to the end of the file
Ccursor_cmd text escape changes from the current position to the cursor command position to text
Search and replace in vi
Command search and replacement operations
/Text search forward text in the file
? Text search for text in the file
N repeated searches in the same direction
N repeated searches in the opposite direction
Ftext looks forward to the current row for text
Ftext searches for text in the current row.
Ttext searches for text in the current row and positions the cursor in the first character of text.
Ttext searches for text in the current row and positions the cursor in the first character of text.
: Case insensitive for set ic search
: Set noic search is case sensitive
: Ranges/pat1/pat2/g replace oldtext with newtext
: M, ns/oldtext/newtext replace oldtext with newtext through n in line m
& Repeat the final: s command
: G/text1/s/text2/text3 search for rows containing text1, replace text2 with text3
: G/text/command: run the commands represented by command in all rows that contain text.
: V/text/command: run the commands represented by command in all rows that do not contain text.
Copy text in vi
Command copy operation
Yy puts the content of the current row into the temporary buffer
Nyy puts n rows of content into the temporary buffer
P put the text in the temporary buffer behind the cursor
P put the text in the temporary buffer before the cursor
"(A-z) nyy copies n rows into a nameable buffer named in parentheses. omitting n indicates the current row.
"(A-z) ndd delete n rows into the nameable buffer named in parentheses. omitting n indicates the current row
"(A-z) p put the content of the nameable buffer named parentheses after the current row
"(A-z) P put the content of the nameable buffer named parentheses before the current row
Undo and repeat in vi
Command undo operation
U undo the last modification
U undo all modifications to the current row
. Repeat the last modification
Repeat the preceding f, F, t, or T search command in the opposite direction.
; Repeat the preceding f, F, t, or T search command
"Np retrieves the last n deletions (buffer contains a certain number of deletions, usually 9)
N repeats the previous/or? Search command
N repeats the previous/or? Command
Save text and exit vi
Command to save and/or exit
: W save the file but do not exit vi
: W file saves the modification in file but does not exit vi
: Wq or ZZ or: x Save the file and exit vi
: Q! Exit vi if you do not save the file
: E! Discard all modifications and start editing the last saved file.
Options in vi
Option function
: Set all print all options
: Set nooption disable option
: Set nu: print the row number before each row
: Set showmode: whether to display the input mode or the replacement mode.
: Set autoindent inherits the indent mode of the first line, which is especially suitable for multi-line comments.
: Set smartindent provides automatic indentation for C programs
: Set list: displays the tab (^ I) and end-of-line characters.
: Set ts = 8. set tab stops for text input.
: Set window = n set text window to show n rows
: Set number: number of rows displayed
: Set nonumber undisplay the number of rows
Vi status
Option function
:. = Print the row number of the current row
: = Print the number of rows in the file
Ctrl + g display the file name, current row number, total number of lines of the file, and the percentage of file locations
: L use the letter "l" to display many special characters, such as tabs and line breaks
Locate paragraphs and placement tags in the text
Option function
{Insert in the first column {to define a paragraph
[Return to the beginning of a paragraph
] Move forward to the beginning of the next paragraph
M (a-z) uses a letter to mark the current position. for example, use mz to Mark z.
'(A-z) move the cursor to the specified tag. for example, use 'Z to move the cursor to z.
Connect rows in vi
Option function
J. connect the next row to the end of the current row
N rows after nJ connection
Cursor placement and screen adjustment
Option function
H move the cursor to the top line of the screen
NH move the cursor to line n at the top of the screen
M move the cursor to the center of the screen
L move the cursor to the bottom line of the screen
NL moves the cursor to line n at the bottom of the screen
^ E (ctrl + e) rolling a row on the screen
Ctrl + y roll a row down the screen
Ctrl + u roll up half page
Ctrl + d roll down half page
Ctrl + B roll one page on the screen
Ctrl + f roll down one page
Ctrl + l re-paint the screen
Z-return: set the current row to the top line of the screen.
Nz-return: Set row n under the current row to the top line of the screen.
Z. set the current row to the center of the screen
Nz. set row n on the current row to the center of the screen
Z-set the current row to the bottom line of the screen
Nz-set row n on the current row to the bottom line of the screen
Shell escape commands in vi
Option function
:! Command :! Ls
:!! Execute the previous shell command
: R! The command reads the input of the command and inserts it, for example, r! Ls will first execute ls and then read the content
: W! Command uses the edited file as the standard input of the command and executes the command, for example, w! Grepall
: Cd directory: change the current working directory to the directory indicated by directory
: Sh starts a sub-shell and returns vi using ^ d (ctrl + d)
: So file: read and execute commands in shell program file
Macros and abbreviations in vi
(Do not use control keys and symbols. do not use character keys K, V, g, q, v, *, =, and function keys)
Option function
: Map key command_seq defines a key to run command_seq. for example, map e ea can always move e to the end of a word to append text.
: Map displays all defined macros in the status line.
: Umap key: macro used to delete the key
: AB string1 string2 defines an abbreviation, so that when string1 is inserted, string1 is replaced with string2. When you want to insert text, type string1 and press Esc. then, string2 is inserted.
: AB show all abbreviations
: Una string cancels the abbreviation of string
Indent text in vi
Option function
When you press ctrl + I or tab to insert text, insert the moving width. the moving width is defined in advance.
: Set ai enable Auto Indent
: Set sw = n set the moving width to n characters
N> make n rows move one width to the right. for example, 3> move one width to the right of the next three rows.