当前位置:网站首页>VIM from dislike to dependence (22) -- automatic completion
VIM from dislike to dependence (22) -- automatic completion
2022-07-01 15:34:00 【aluluka】
In this article, we will discuss vim Self contained automatic completion function . Of course , There are many plug-ins for automatic completion , But understand vim The built-in function helps us better complete the function of plug-ins . Because I have seen some configuration files that configure the functions of plug-ins more difficult to use than the original , And only using basic functions may not be easy to use the original . So here is the usage of the original version , It can be regarded as helping you provide a benchmark in the future configuration .
make command
Before you know about automatic completion , Let's talk briefly first :make
This command , It is similar to that introduced in the previous article :grep
Command similar , It's also true shell
A package of commands . By default, it encapsulates make
command .
We are right. c/c++
Language execution :make
That is, calling shell
Medium make
command . It will store the error information generated by compilation in quickfix
In the list . We introduced how to operate in the previous section quickfix
list . It also introduces how to :grep
Command transformation . alike :make
It also supports the transformation using the same method .
:make
In command , Use makeprg
To execute external commands , Use errorformat
To format output to quickfix
in . Their default values are as follows :
makeprg="make"
errorformag="errorformat=%*[^"]"%f"%*\D%l: %m,"%f"%*\D%l: %m,%-G%f:%l: (Each undeclared identifier is reported only once,%-G%f:%l:
for each function it appears in.),%-GIn file included from %f:%l:%c:,%-GIn file included from %f:%l:%c\,,%-GIn file incl
uded from %f:%l:%c,%-GIn file included from %f:%l,%-G%*[ ]from %f:%l:%c,%-G%*[ ]from %f:%l:,%-G%*[ ]from %f:%l\,,%-G%*[
]from %f:%l,%f:%l:%c:%m,%f(%l):%m,%f:%l:%m,"%f"\, line %l%*\D%c%*[^ ] %m,%D%*\a[%*\d]: Entering directory %*[`']%f',%X%*
\a[%*\d]: Leaving directory %*[`']%f',%D%*\a: Entering directory %*[`']%f',%X%*\a: Leaving directory %*[`']%f',%DMaking
%*\a in %f,%f|%l| %m"
Their values can be adjusted to suit different external commands . I'm not going to go into details here , I believe that the little friend who has read the last article should not be unfamiliar with this . Originally :make
The order is vim A very useful command in , Should write a separate article . But it's in :grep
It's too repetitive , So I decided to go over it when I introduced other contents . For more information, please refer to vim User's manual .
Automatic completion
Automatic completion can be triggered in insert mode , When we trigger the completion function ,vim A completion list will be established according to the contents of all buffers in the current editing session , Then detect according to the characters on the left side of the current cursor , See if you can find a part of the word in the table , If you can find it, you will filter the completion list with this unfinished word , So not all words that begin with it are filtered out , The rest form a pop-up menu for users to choose . The effect is as follows :
In the above example , Because in order to re
At the beginning, there was only require
a , To show the completion effect, here we add a new one with re
At the beginning return
We use <Ctrl +p>
and <Ctrl + n>
To switch between the previous and next items in the completion menu . In addition to this , We also have other shortcut keys for operating the completion menu .
<Ctrl - n>
: Use the next item from the completion list (next)<Ctrl - p>
: Use the previous item from the completion list (prev)<Down>
: And<Ctrl -n>
identical<Up>
: And<Ctrl - p>
identical<Ctrl -y>
: Confirm to use the currently selected match<Ctrl - e>
: Restore the original entry<Ctrl -h>
: Delete a character from the current match<Ctrl - l>
: Add a character from the current match
Usually when inputting characters , If there is a match to match vim It will pop up automatically , Or you can use it manually <Ctrl - n>
Pop up the match menu . After determining the match to be used, you can use <Ctrl-y>
To confirm
Sometimes, although the menu of matching items pops up , But there are too many matches , And the words you need are too late in the list , It can be used at this time <Ctrl - e>
To exit the menu , Manually enter a few characters to make the match more accurate . Or you can enter <Ctrl -p>
Get to the first item , That is, our current input , Then enter the characters again to simplify the menu items , Then use <Ctrl - n>
Pop up menu . Use this method to approach the desired result step by step
Custom completion item source
By default ,vim The complete items mainly come from the following places :
- Buffer list :vim The most basic source of completion is the current buffer list . It can go through
<Ctrl - x><Ctrl - n>
To trigger the item . - Include files , All programming languages have the concept of including files , for example
c/c++
Medium#include
,python
Mediumimport
. Use<Ctrl-x><Ctrl-i>
This option can be triggered , Give Way vim Extract the completion item from the included file .vim Self usec
language-written , It can recognizec/c++
Keywords in language , We can specifyinclude
Item to make vim Know other different keywords . Commonly used programming languages vim Can recognize , So there is no need to modifyinclude
term . - Label file , We use
ctags
Or similar plug-ins will generate a label file , This file will scan the keywords in the code 、 function 、 The index of variables, etc. is put into a file for subsequent jump . At the same time, they will also generate a series of completion items into the completion list . have access to<Ctrl+x><Ctrl+]>
To trigger
It is usually used directly <Ctrl + n>
What triggers is the completion item in the current buffer list , Use <Ctrl+x>
As a prefix , Other types of completion items can be triggered . One advantage of doing this is to try to simplify and complete the list , It reduces the process of manual traversal . But sometimes we don't know where the content I want comes from , Is there any way to do it , use <Ctrl + n>
This button can call the completion items of all other sources ?
To do that , have access to complete
This configuration item . This item contains a set of parameters represented by single characters separated by commas , When a parameter appears, it means that the position represented by the parameter needs to be scanned . Use set complete?
You can see , The default is complete=.,w,b,u,t
. We can use set complete-=i
perhaps set complete+=k
To delete or add a scanning location . Common location parameters are as follows :
.
: Represents the buffer currently openw
: Currently open windowb
: Current buffer listu
: Currently in the buffer list , But the unopened buffert
: Current label fileU
: Currently open , Not a buffer in the buffer listk
: The complement loaded from the dictionary filei
: Read from the current file and the include filed
: Read and use from the current file and the include file define Defined macro
Complete content can be used :h 'complete'
Check it out. .
Use dictionary files
In the above discussion , We can know vim It is a dictionary file that can be customized and completed , Then generate matching from the dictionary . We can use <Ctrl-x><Ctrl-k>
To load matches in the dictionary .
We can use set spell To start spell checking , Spell checking also produces new dictionary files . If you don't want to use this , You can also use set dictionary To specify a dictionary file containing one or more words .
In this case, we are nvim-config
Create a new one in the directory spell.txt
file , We write the following in it
require
return
request
And then use set dictionary=./spell.txt
, Then delete init.lua
Medium return
, Input re
And then use <Ctrl+x><Ctrl+k>
At this time, we found that it had been loaded
Complete the whole line
In addition to completing words ,vim You can also complete the whole line , Use <Ctrl+x><Ctrl+l>
It can trigger the operation of completing the whole line .
The source of the completion item in the completion line is the same as the completion word , It should be noted that the indentation at the beginning of the line will be automatically ignored when completing the line .
The operation of the completion line is the same as that introduced before yy
perhaps :t
The effect is the same , We should use them separately according to the actual situation .
Complete the file name
stay shell You can use <Tab>
Key to automatically complete the file path ,vim Use in <Ctrl+x><Ctrl+f>
To complete the file path and file name .
It should be noted that when we use relative paths to complete file names , Working directory is used , That is, from which directory you entered vim. We can do it in vim Use in :cd To switch working directories . For example, I am in nvim-config The root directory of this project is executed nvim init.lua, In this document, we hope to complete it quickly basic/settings.lua the , We found that it reported an error when completing
We can use it at this time :cd lua
To switch the working directory to nvim-config/lua
. At this time, it is OK to execute the completion order .
Generate complements according to specific programming languages
The above complement is a little useful when editing ordinary text , But as a programmer, if you can only use the above methods to complete the code, you will be crazy . Fortunately vim Provides something like other IDE That kind of completion method based on programming language . The shortcut key for using this completion method is <Ctrl+x><Ctrl+o>
To enable this method , Need to start file type recognition .
nvim This feature has been enabled in , Therefore, there is no need to deliberately set , But here I still give its configuration .
vim.o.filetype="plugin"
perhaps vim The following code can be used in
set filetype=plugin
set nocompatiable # Set up with vi Are not compatible
For example, we can try in css
Use completion in the document
vim It does support the automatic completion of many languages , However, in order to get a complete experience, it is recommended to use various special completion plug-ins to get a better experience
Final summary
In this article , It introduces vim The main sources of the Chinese complement are : Current buffer and buffer list 、 Include files 、 Tags generated by external programs, etc . At the same time, it also introduces how to use shortcut keys to complete different items , Now these shortcuts are summarized as follows :
<Ctrl + n>
: Complete common keywords , Mainly from the buffer list and the current buffer<Ctrl+x><Ctrl+n>
: And <Ctrl+n> The same effect<Ctrl+x><Ctrl+i>
: Get the completion item from the include file<Ctrl+x><Ctrl+]>
: Get the completion item from the external tag<Ctrl+x><Ctrl+k>
: Get the completion item from the dictionary file<Ctrl+x><Ctrl+l>
: Complete the whole line<Ctrl+x><Ctrl+f>
: Complete the file name<Ctrl+x><Ctrl+o>
: Complete according to the programming language
边栏推荐
- Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
- 《QT+PCL第六章》点云配准icp系列6
- Microservice tracking SQL (support Gorm query tracking under isto control)
- Qt+pcl Chapter 9 point cloud reconstruction Series 2
- 《QT+PCL第六章》点云配准icp系列4
- vim 从嫌弃到依赖(22)——自动补全
- Skywalking 6.4 distributed link tracking usage notes
- Hardware design guide for s32k1xx microcontroller
- Go zero actual combat demo (I)
- Tableapi & SQL and Kafka message acquisition of Flink example
猜你喜欢
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
Survey of intrusion detection systems:techniques, datasets and challenges
Redis high availability principle
如何实现时钟信号分频?
【目标跟踪】|STARK
skywalking 6.4 分布式链路跟踪 使用笔记
MySQL 服务正在启动 MySQL 服务无法启动解决途径
Qt+pcl Chapter 9 point cloud reconstruction Series 2
Wechat applet 02 - Implementation of rotation map and picture click jump
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
随机推荐
Qt+pcl Chapter 9 point cloud reconstruction Series 2
Fix the failure of idea global search shortcut (ctrl+shift+f)
重回榜首的大众,ID依然乏力
go-zero实战demo(一)
Photoshop plug-in HDR (II) - script development PS plug-in
贝联珠贯加入龙蜥社区,共同促进碳中和
skywalking 6.4 分布式链路跟踪 使用笔记
Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
如何写出好代码 - 防御式编程指南
Redis秒杀demo
Survey of intrusion detection systems:techniques, datasets and challenges
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
MySQL高级篇4
The difference between arrow function and ordinary function in JS
Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization