

VIM学习笔记 匹配成对字符(Match Pairs)
source link: http://yyq123.blogspot.com/2020/09/vim-matchpairs.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Originally from Practical Vim by Drew Neal
'matchpairs'选项
matchpairs选项,用来控制哪些字符可以通过%
命令进行匹配。此选项的默认值如下:
:set matchpairs=(:),{:},[:]
也就是说,在开括号“(,{,[”上点击%
键,将会自动跳转到对应的闭括号“),},]”上;同理,在闭括号上点击%
键,也会跳转回到对应的开括号上;同时,匹配跳转也能够正确处理括号嵌套的情况。
如果当前光标下并非括号,那么点击%
键,将自动在本行内向前查找并定位到括号之上。
Source: https://catonmat.net/vim-plugins-matchit-vim
如果需要新增匹配类型,例如增加对于HTML文件中的尖括号的匹配,那么可以使用以下命令:
:set mps+=<:>
利用自动命令(autocmd),可以针对特定文件类型设置匹配字符。例如针对C和Java代码,增加对于“=”和“;”的匹配:
:au FileType c,cpp,java set mps+==:;
'showmatch'选项
如果希望在输入闭括号时,短暂地跳转到与之匹配的开括号,那么可以设置以下选项:
:set showmatch
'matchtime'选项
'matchtime'选项,用于控制显示配对括号的时间,其单位为0.1秒,默认值为5,即0.5秒。
如果希望持续显示配对括号1.5秒,那么可以使用以下命令:
:set matchtime=15
matchit插件
matchit插件扩展了%
命令的功能,支持if/else/endif语法结构;支持HTML标签。使用:help matchit-languages
命令,可以查看当前支持的所有语言列表。
从Vim 6.0开始,matchit插件伴随vim发行,内置于$VIMRUNTIME\pack\dist\opt\matchit
目录中,并不需要单独安装。
在vimrc配置文件中增加以下命令,可以启用matchit插件:
packadd! matchit
在HTML标签中,点击%
键,将移动到关闭标签上:
Source: https://catonmat.net/vim-plugins-matchit-vim
使用以下命令,可以查看更多帮助信息:
:help matchit
命令小结
%
跳转到匹配字符
:set matchpairs
设置形成配对的字符
:set showmatch
设置是否短暂跳转到匹配括号
:set matchtime
设置显示配对括号的时间长度
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK