- One . About vim Editor
- Two .vim Editor Mode
- 3、 ... and . Basic operations in general mode
- Four .V Pattern ( Column mode ) Basic operation of
- 5、 ... and . Basic operations in command mode
- 6、 ... and . Customize vim Environmental Science
- 7、 ... and .vim Open multiple files at the same time
- 8、 ... and . Compare the contents of the two files
One . About vim Editor
Vim Is a similar to Vi The famous and powerful 、 Highly customizable text editor , stay Vi Many features have been improved and added .VIM It's free software .vim Can be viewed as
vi Updated version of , He can display special information in a variety of colors .
[[email protected] ~]# rpm -qf `which vim`
vim-enhanced-7.4.160-5.el7.x86_64
[[email protected] ~]# rpm -qf `which vi`
vim-minimal-7.4.160-2.el7.x86_64
# You can see from the output ,vim yes vi Added version of , The most obvious difference is vim Grammar can be highlighted , It's fully compatible vi
Two .vim Editor Mode
1.vim The editor has three modes , The first entry is in general mode , In the lower left corner “Insert” It's editing mode , Input : It's command line mode .
- How to switch from edit mode to command line mode ? Edit mode ->esc-> General pattern ->: -> Command mode
- How does general mode enter editing mode ? Press a or i or o or A or I or O

2. summary :vim How to enter other modes ?
- a A o O i I All of them can be inserted , Edit mode
- : Enter command line mode
- v Enter visual mode
- ctrl+v Enter visual block mode
- V Enter visual line mode
- R erase 、 rewrite , Go to replace mode
- When you enter the above mode , Want to quit , Press esc
3、 ... and . Basic operations in general mode
i Insert... Before the current character ( In front of the cursor )
I Insert... At the beginning of the line ( Head of line )
a Insert... After the current character ( After the cursor )
A Insert at the end of the line ( At the end of the line )
o Insert... In the next line ( Another line )
O Insert... In the previous line ( Insert... In the previous line )
x Delete one character back Equate to delete
X Delete a character forward
u Undo one step Cancel every time you press
r Replace , "r" Command is not an operator command . It waits for you to type the next character to replace the character under the current cursor ."r" A command count before a command is to replace multiple characters with the character to be entered . To replace a character with a newline character, use "r". It will delete a character and insert a newline character . Using command counting here will only delete the specified number of characters :"4r" Will put 4 Replace characters with a newline character .
# Cursor positioning
hjkl Lower left, upper right
0 and home Key to switch to the beginning of the line , $ and end Key to switch to end of line
gg Quickly navigate to the first line of the document , G Go to no line
3gg perhaps 3G Fast track to 3 That's ok
/string( character string ) ----- Find or locate the word or content you are looking for , If there are more matches , We can go through N、n To search up and down , also vi Will highlight what you find , Cancel using :noh
/^d ----^ What does the meaning begin with ,, Look for letters d Content at the beginning
/t$ -----$ What is the end of meaning ,, Look for letters t Ending content
vim + a.txt After opening the file , The cursor will automatically be on the last line of the file
# Edit the text
# Delete 、 Copy 、 Paste 、 revoke
y Copy ( In characters ): Means to copy a single character , If you want to copy the whole line , use yy( Behavior unit ),"y" The operator command copies the text to a register 3 in . And then you can use "p" Order it back . because "y" It's an operator command , So you can use "yw" To make a copy of word. You can also use the command to count . In the following example "y2w" Command to copy two word,"yy" Command to copy a whole line ,"Y" It's also copying the entire line , The command to copy the current cursor to the end of a line is "y$".
Copy N That's ok : Nyy , such as : 2yy , Represents replication 2 That's ok
dd( Delete , Behavior unit , Delete the current cursor line )
Delete N That's ok : Ndd , such as : 2dd , Said to delete 2 That's ok
p : P Paste
shear : dd
x Deletes the character where the cursor is located
D Delete from cursor to end of line
u Cancel the operation
ctrl+r Restore undone operations , Restore the undo operation back to , That is to say, what is it like before the cancellation , What would it look like again
r Replace , Or to modify a character , "r" Command is not an operator command . It waits for you to type the next character to replace the character under the current cursor ."r" A command count before a command is to replace multiple characters with the character to be entered . To replace a character with a newline character, use "r". It will delete a character and insert a newline character . Using command counting here will only delete the specified number of characters :"4r" Will put 4 Replace characters with a newline character .
Four .V Pattern ( Column mode ) Basic operation of
1. Get into v Pattern Move the cursor to select the area , The steps of making multi line comments when programming :
- ctrl+v Enter column edit mode
- Move the cursor down or up , Put the need to comment on 、 Select the beginning of the edit line
- If you need to delete , Just type x Key to delete
- If you want to add content , Just press in capitals I
- Then insert the comment or the symbol you need to insert , such as "#"
- Press again Esc, It's all annotated or added
2. Delete : Press again ctrl+v Enter column edit mode ; Move the cursor down or up ; Select the comment section , Then press d, It will delete the comment symbol .
5、 ... and . Basic operations in command mode
:w preservation save
:w! Force save
:q No changes have been made , sign out quit
:q! Revised , Do not save , Forced exit
:wq Save and exit
:wq! Force save and exit
:x Save and exit
# Call an external file or command
hypothesis : I want to write to my network card MAC Address , I want to check , The current in vim Edit document , Copywriting . It's so troublesome .
Operate in command line mode :
:!ifconfig Invoking system commands
!+ command
Read other files .( Append the contents of other files to the current document )
:r /etc/hosts
# Text substitution
# Format : Range ( among % All contents ) s Separator Old content Separator New content ( Separators can be customized )
# Default is the first qualified word in each line (/g All )
:1,3 s/bin/xuegod # Replacement section 1 To 3 The first one in the line bin Replace with xuegod
:1,3 s/bin/xuegod/g # Replacement section 1 To 3 Find all the bin Replace with xuegod
:3 s/xue/aaaaa # Just put the 3 The line is replaced by
:% s/do/xuegod/g # Put all the do Replace with xuegod
:% s/do/xuegod/gi # Put all the do Replace with xuegod, And ignore do The case of
:% [email protected]@[email protected] # Put all the a Replace with b
6、 ... and . Customize vim Environmental Science
You can customize it according to your preference vim Environmental Science .
# Temporary settings vim Environmental Science
:set nu # set nu
:set nonu # Unset line number
:noh # set nohlsearch
# Permanent settings vim Environmental Science
[[email protected] ~]#vim /etc/vimrc # After setting, it will affect all users of the system
[[email protected] ~]#vim ~/.vimrc # In the user's home directory , Create a .vimrc. This only affects one user , I didn't build one myself
example :
[[email protected] ~]# cat /root/.vimrc
set nu
7、 ... and .vim Open multiple files at the same time
1. Above and below , Open two documents , Use ctrl+ww Switch editing between two documents
# Lowercase o Split up and down
[[email protected] ~]#vim -o /etc/passwd /etc/hosts
2. Open two documents left and right , Use ctrl+ww Switch editing between two documents
# Capitalization O Left and right split screen
[[email protected] ~]#vim -O /etc/passwd /etc/hosts
8、 ... and . Compare the contents of the two files
There are two ways to compare the contents of two files :diff and vimdiff.
[[email protected] ~]#cp /etc/passwd mima.txt
[[email protected] ~]#echo aaa >> mima.txt
[[email protected] ~]#diff /etc/passwd mima.txt
40a41
> aaa
[[email protected] ~]#vimdiff /etc/passwd mima.txt