当前位置:网站首页>[vi/vim] basic usage and command summary
[vi/vim] basic usage and command summary
2022-06-28 12:16:00 【Jia Pu】
vi/vim Basic use and command summary of
The goal is
vibrief introduction- Open and create new files
- Three working modes
- Common commands
- Split screen command
- Common command quick look up chart
01. vi brief introduction
1.1 Study vi Purpose
- At work , Right The server On the file Simple Modification of , have access to
sshLog on to the server remotely , And useviYou can edit it quickly - Common files that need to be modified include :
- Source program
- The configuration file , for example
sshConfiguration file for~/.ssh/config
- In the absence of a graphical interface , To edit a file ,
viIs the best choice !- Each one should use Linux The programmer , We should learn more or less
viCommon commands
1.2 vi and vim
- In many
LinuxDistribution in , Put... DirectlyviMake itvimSoft connection
vi
viyesVisual interfaceFor short , yesLinuxin The most classic Text editor forviThe core design idea of —— Keep the programmer's fingers in the core area of the keyboard , You can do all the editing
viCharacteristics :- There is no graphical interface Of Powerful Editor for
- Only editors Text content , You can't change the font 、 Arrange paragraphs
- Mouse operation not supported
- No menu
- Only the command
viThe editor is in System management 、 Server management When editing a file , Its function can never be compared with the editor of graphical interface
vim
vim = vi improved
vimIt's fromviDeveloped a text editor , Support Code completion 、 compile And Wrong jump And other convenient programming functions are particularly rich , Widely used in programmers , go by the name of The God of editor
Query the soft connection command ( know )
- In many
LinuxIn the distribution version, theviMake itvimSoft connection
# lookup vi Run file
$ which vi
$ ls -l /usr/bin/vi
$ ls -l /etc/alternatives/vi
$ ls -l /usr/bin/vim.basic
# lookup vim Run file
$ which vim
$ ls -l /usr/bin/vim
$ ls -l /etc/alternatives/vim
$ ls -l /usr/bin/vim.basic
02. Open and create new files
- Input in the terminal
viFollowed by the file name that will do
$ vi file name
- If the file already exists , Will open the file directly
- If the file doesn't exist , A new file will be created
2.1 Open the file and locate the line
In daily work , It's possible to meet Open a file , And navigate to the specified line The situation of
for example : At development time , Know that a line of code has errors , Sure Rapid positioning To the location of the error code
This is the time , You can use the following command to open a file
$ vi file name + Row number
Tips : If you only take
+Instead of specifying the line number , Will go directly to the end of the file
2.2 exception handling
- If
viAbnormal exit , There may be Swap file - Next time
viWhen editing the file , You will see the following screen information , Press the letterdSure Delete interchange file that will do
Tips : When you press the keyboard , Pay attention to turn off the input method

03. Three working modes
viThere are three basic modes of work :- Command mode
- Open the file first into command mode , It's using
viOf entrance - adopt command Routine editing of files , for example : location 、 Page turning 、 Copy 、 Paste 、 Delete ……
- In other graphic editors , adopt Shortcut key perhaps mouse Operations implemented , All in Command mode achieve
- Open the file first into command mode , It's using
- Last row mode —— perform preservation 、 sign out Wait for the operation
- To exit
viBack to the console , You need to enter a command in last line mode - Last row mode yes
viOf exit
- To exit
- Edit mode —— Normal editing text

- Command mode
Tips : stay
Touch BarOf Mac On the computer , PressESCinconvenient , have access toCTRL + [replace
Last mode command
| command | english | function |
|---|---|---|
| w | write | preservation |
| q | quit | sign out , If you don't save it , You are not allowed to exit |
| q! | quit | To force out of , Exit without saving |
| wq | write & quit | Save and exit |
| x | Save and exit |
04. Common commands
Command wiring diagram
- Repeat the number
- In command mode , Enter a number first , Follow another command , You can make the order Repeat the specified number of times
- Move and choose ( More practice )
viThe reason is fast. , The key lies in Be able to quickly locate the line of code to be edited- Mobile command can and Edit operation command Use a combination of
- Edit operation
- Delete 、 Copy 、 Paste 、 Replace 、 Indent
- Revocation and repetition
- Search and replace
- edit
Learning tips
viThere are more orders for , Don't expect to remember all at once , Individual orders forget , It just affects the editing speed- In the use of
viOn command , Be careful Turn off Chinese IME
4.1 Move ( basic )
- Use it skillfully
vi, First of all, we should learn how to Command mode Move the cursor quickly in the following example - Edit operation command , Can and Mobile command To use together
1) On 、 Next 、 Left 、 Right
| command | function | finger |
|---|---|---|
| h | towards the left | index finger |
| j | Down | index finger |
| k | Up | Middle finger |
| l | towards the right | ring finger |

2) Move in line
| command | english | function |
|---|---|---|
| w | word | Move a word back |
| b | back | Move a word forward |
| 0 | Head of line | |
| ^ | Head of line , The first position is not a blank character | |
| $ | At the end of the line |
3) Rows move
| command | english | function |
|---|---|---|
| gg | go | Top of file |
| G | go | end of file |
| Numbers gg | go | Move to Numbers Number of corresponding lines |
| Numbers G | go | Move to Numbers Number of corresponding lines |
| : Numbers | Move to Numbers Number of corresponding lines |
4) Screen movement
| command | english | function |
|---|---|---|
| Ctrl + b | back | Page up |
| Ctrl + f | forward | Page down |
| H | Head | At the top of the screen |
| M | Middle | In the middle of the screen |
| L | Low | At the bottom of the screen |
4.2 Move ( Program )
1) Paragraph move
viUse in Blank line To distinguish paragraphs- In program development , Usually A piece of function related code will be written together —— There is no blank line between
| command | function |
|---|---|
| { | Last paragraph |
| } | Next paragraph |
2) Bracket toggle
- In the world of programs ,
()、[]、{}The frequency of use is very high , and They come in pairs
| command | function |
|---|---|
| % | Bracket matching and switching |
3) Mark
- At development time , A piece of code may It needs to be handled later , for example : edit 、 see
- Use... First
mAdd a marker , This can Jump back quickly when needed perhaps Perform other editing operations - Tag name It can be
a~zperhapsA~ZIn between One Letter - Marked If the line is deleted , The tag is also deleted
- If Tag with same name added to other lines , Previously added tags will also be replaced
| command | english | function |
|---|---|---|
| mx | mark | Add tag x,x yes a~z perhaps A~Z Any letter between |
| 'x | Go straight to the mark x The position of |
4.3 Select the text ( Visual Modes )
- Study
CopyBefore the command , We should learn to How to choose Code to copy - stay
viChoose the text , You need to use it firstVisualCommand switch to Visual Modes viProvided in Three Visual Modes , It is convenient for programmers to choose How to select text- Press
ESCYou can give up the selection , Back to Command mode
| command | Pattern | function |
|---|---|---|
| v | Visual Modes | Select the text in normal mode from the cursor position |
| V | Visual line mode | Select the full line the cursor passes through |
| Ctrl + v | Visual block mode | Select text vertically |
- Visual Modes Next , You can talk to Mobile command Continuous use , for example :
ggVGBe able to select everything
4.4 Revocation and reinstatement of revocation
- Before learning editing commands , First of all, you need to know how to undo the previous FALSE Edit actions !
| command | english | function |
|---|---|---|
| u | undo | Undo the last order |
| CTRL + r | redo | To resume an order of revocation |
4.5 Delete text
| command | english | function |
|---|---|---|
| x | cut | Delete cursor character , Or select the text |
| d( Mobile command ) | delete | Delete the content corresponding to the move command |
| dd | delete | Deletes the line where the cursor is located , Sure ndd Copy many lines |
| D | delete | Delete to end of line |
Tips : If you use Visual Modes A text has been selected , So no matter what you use
dstillx, Can delete the selected text
- The delete command can be used with Mobile command Continuous use , Here are the common combined commands :
* dw # Delete from cursor position to end of word
* d0 # Delete from cursor position to the beginning of a line
* d} # Delete from the cursor position to the end of the paragraph
* ndd # Delete continuously from the cursor down n That's ok
* d Lines of code G # From the line where the cursor is Delete to Specify the line of code All code between
* d'a # From the line where the cursor is Delete to Mark a All code between
4.6 Copy 、 Paste
viThere is a Buffer for copied text- Copy The command saves the selected text in the buffer
- Delete Command deleted text will be saved in the buffer
- Where needed , Use Paste The command can insert the text of the buffer into the position of the cursor
| command | english | function |
|---|---|---|
| y( Mobile command ) | copy | Copy |
| yy | copy | Duplicate a row , Sure nyy Copy many lines |
| p | paste | Paste |
Tips
- command
d、xSimilar to the graphic interface Cut operation ——CTRL + X - command
ySimilar to the graphic interface Copy operation ——CTRL + C - command
pSimilar to the graphic interface Paste operation ——CTRL + V viMedium There is also only one text buffer , If I do it later Copy 、 shear operation , The contents of the previous buffer will be replaced
Be careful
viMedium Text buffer And systematic clipboard Not the same- So use... In other software
CTRL + CCopied content , Can't be inviPass throughPCommand paste - Can be in Edit mode Next use Right click to paste
4.7 Replace
| command | english | function | Working mode |
|---|---|---|---|
| r | replace | Replace the current character | Command mode |
| R | replace | Replace the character after the current line cursor | Replace mode |
ROrders can enter Replace mode , When the replacement is complete , Press downESCYou can go back to Command mode- Replace the command The function of is not to enter Edit mode , On the file Lightweight modifications
4.8 Indent and repeat
| command | function |
|---|---|
| >> | Increase indent to the right |
| << | Decrease indent to the left |
| . | Repeat the last order |
- Indent order When developing programs , Add indents to the code More useful !
- Disposable Add... Before selecting code 4 A space , It's called Increase Indent
- Disposable Delete... Before selecting code 4 A space , It's called Reduce indent
- stay Visual Modes Next , The indent command only needs to use One
>perhaps<
In the program , Indent Usually used to represent the ownership of code
- The fewer spaces in front , The higher the level of code
- The more spaces ahead , The lower the level of code
4.9 lookup
Regular search
| command | function |
|---|---|
| /str | lookup str |
- After finding the specified content , Use
NextFind the next place to appear :n: Find the next oneN: Find the last one
- If you don't want to see highlights , You can find any content that does not exist in a file
Word quick match
| command | function |
|---|---|
| * | Find the word of the current cursor backward |
| # | Look forward to the word where the current cursor is |
- In development , Quickly match words , You can quickly see where the word has been used in other places
4.10 Find and replace
- stay
viFind and replace commands in Last row mode perform - Memory command format :
:%s///g
1) Global replacement
- Disposable Replace... In the file All the old texts that appear
- The command format is as follows :
:%s/ Old text / New text /g
2) Visual area replacement
- Select first To replace the words Range
- The command format is as follows :
:s/ Old text / New text /g
3) Confirm replacement
- If you put the last
gChange togcWhen replacing , There will be hints. ! Recommended !
:%s/ Old text / New text /gc
y-yesReplacen-noDo not replacea-allReplace allq-quitQuit replacingl-lastthe last one , And move the cursor to the beginning of the line^EScroll down^YScroll up
4.11 Insert command
- stay
viIn addition to the commonly usediGet into Edit mode Outside , The following commands are also provided to enter the editing mode :
| command | english | function | Commonly used |
|---|---|---|---|
| i | insert | Insert text before current character | Commonly used |
| I | insert | Insert text at the beginning of the line | More commonly used |
| a | append | Add text... After the current character | |
| A | append | Add text at the end of the line | More commonly used |
| o | Insert a blank line after the current line | Commonly used | |
| O | Insert a blank line before the current line | Commonly used |

rehearse 1 —— Edit commands and numbers
- In development , Continuous input may be encountered
NThe same characters
stay
PythonThere are simple ways , But in other languages, you usually need to input by yourself
- for example :
**********continuity 10 asterisk
To achieve this effect can be found in Command mode Next
- Input
10, To repeat 10 Time - Input
iGet into Edit mode - Input
*That is, repeated words - Press down
ESCBack to Command mode , After returningviWill put the second2、3Repeat the two steps10Time
Tips : In normal development , stay Before entering edit mode , Don't press the number
rehearse 2 —— utilize Visual block Add comments to multiple lines of code
- In development , You may encounter multiple lines of code at once Add notes The situation of
stay
Pythonin , To add comments to the code , You can add a... In front of the code#
To achieve this effect can be found in Command mode Next
- Move to the The first 1 Line code , Press
^Come to the head of the line - Press
CTRL + vGet into Visual block Pattern - Use
jSelect the lines of code you want to add in a row - Input
IGet into Edit mode , And in Insert... At the beginning of the line , Be careful : Be sure to use I - Input
#That is, annotation symbols - Press down
ESCBack to Command mode , After returningviEvery line of code that will be selected before front Insert#
05. Split screen command
- Belong to
viHigh order of —— Sure Edit and view multiple files at the same time
5.1 Last line command extension
Last command It is mainly for file operation : preservation 、 sign out 、 preservation & sign out 、 Search for & Replace 、 Save another 、 newly build 、 browse files
| command | english | function |
|---|---|---|
| :e . | edit | Will open the built-in file browser , Browse the files in the current directory |
| :n file name | new | New file |
| :w file name | write | Save as , But still edit the current file , Does not switch files |
Tips : Before switching files , You must ensure that the current file has been saved !
- I have learned Last command :
| command | english | function |
|---|---|---|
| :w | write | preservation |
| :q | quit | sign out , If you don't save it , You are not allowed to exit |
| :q! | quit | To force out of , Exit without saving |
| :wq | write & quit | Save and exit |
| :x | Save and exit | |
| :%s///gc | Confirm search and replace |
In actual development , have access to
wcommand Staged backup code
5.2 Split screen command
- Use Split screen command , Sure Edit and view multiple files at the same time
| command | english | function |
|---|---|---|
| :sp [ file name ] | split | Increase split screen horizontally |
| :vsp [ file name ] | vertical split | Add split screen vertically |
1) Switch the split screen window
Split windows are based on
CTRL + WThis shortcut is ,wThe corresponding English word iswindow
| command | english | function |
|---|---|---|
| w | window | Switch to the next window |
| r | reverse | Swap windows |
| c | close | Close the current window , But you can't close the last window |
| q | quit | Exit the current window , If it's the last window , Then close vi |
| o | other | Close other windows |
2) Resize window
Split windows are based on
CTRL + WThis shortcut is ,wThe corresponding English word iswindow
| command | english | function |
|---|---|---|
| + | Increase window height | |
| - | Reduce window height | |
| > | Increase window width | |
| < | Reduce window width | |
| = | Bisect window size |
The command to adjust the width and height of the window can be used with numbers , for example :
5 CTRL + W +continuity 5 Increase the height for times
06. Common command quick look up chart

vimrc
vimrcyesvimConfiguration file for , You can set vim Configuration of , Include : Hot key 、 Color matching 、 Syntax highlighting 、 plug-in unit etc.LinuxinvimrcThere are two positions , The configuration files in the home directory have higher priority
/etc/vim/vimrc
~/.vimrc
- Common plug-ins are :
- Code completion
- Code folding
- Search for
- Git Integrate
- ……
- There are many experts on the Internet that have been configured for
pythonDevelopedvimrcfile , You can download it and use it directly , Or wait for everyoneLinuxAfter getting familiar with it , Relearn !
边栏推荐
- Leetcode 705. 设计哈希集合
- Simulation of the Saier lottery to seek expectation
- If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
- Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
- 已知两个点和中间一个比例的点,求该点坐标
- 智联招聘基于 Nebula Graph 的推荐实践分享
- 2. single digit statistics
- Map排序工具类
- Privilege management of vivo mobile phone
- Leetcode 48. 旋转图像(可以,已解决)
猜你喜欢
Using MySQL database in the express framework of node

Day33 JS note event (Part 2) September 28, 2021

Data analysis learning notes

【C语言】NextDay问题

2018 joint examination of nine provinces & Merging of line segment trees

RemoteViews布局和类型限制源码分析

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

Redis principle - List

Simulation of the Saier lottery to seek expectation

建立自己的网站(18)
随机推荐
NFT card chain game system development DAPP construction technical details
Mutual conversion between mytipartfile and file
期货开户有门槛吗,如何网上安全的开通期货账户
面试步骤的面试技巧
Zero basic C language (I)
Using MySQL database in the express framework of node
开源项目维权成功案例: spug 开源运维平台成功维权
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
SoapUI rookie tutorial
PrecomputedTextCompat用法及原理
Self use demo of basic component integration of fluent
Prefix and (one dimension)
MapReduce项目案例1
AcWing 607. Average 2 (implemented in C language)
Day30 JS notes BOM and DOM 2021.09.24
Day23 JS notes 2021.09.14
【北京航空航天大学】考研初试复试资料分享
【C语言】NextDay问题
Dongyuhui, New Oriental and Phoenix Satellite TV
水果FL Studio/Cubase/Studio one音乐宿主软件对比