简体   繁体   English

将VIM命令安装为插件

[英]Installing VIM command as a plugin

I have the following command run as a vim plugin under ~/.vim/plugin/autohighlight.vim 我在〜/ .vim / plugin / autohighlight.vim下将以下命令作为vim插件运行

:autocmd CursorMoved * exe printf('match IncSearch /\\<%s\\>/', expand('<cword>'))

The thing is, it throws a bunch of errors anywhere except when I'm editing a file. 事实是,除了我正在编辑文件时,它在任何地方都会引发很多错误。 (file Explorer, other windows) (文件资源管理器,其他窗口)

Is there a way to tell the script to only take effect when a file is edited and not anywhere else in VIM? 有没有办法告诉脚本仅在文件被编辑时才生效,而在VIM中的其他地方不生效?

You can try this one: 您可以尝试以下一种方法:

autocmd CursorMoved * :if filewritable(@%)==1 |
                      \    call matchadd('IncSearch', '\V\<'.escape(expand('<cword>').'\>', '\'), 10, 1) |
                      \endif

It will do highlighting only if current buffer name is a writable file (not directory). 仅当当前缓冲区名称是可写文件(而不是目录)时,它才会突出显示。 It will fail if you are editing something which uses BufWriteCmd to perform saving (for example, if you are editing file inside a zip archive). 如果您正在编辑使用BufWriteCmd进行保存的内容(例如,如果您正在zip压缩文件中编辑文件),它将失败。

By the way, can you provide these errors? 顺便问一下,您能提供这些错误吗? I was able to get an error when I used :e . 当我使用:e .时,我能够得到一个错误:e . , but it was not related to the fact that you are observing a directory, it appeared just because you forgot to do escaping . ,但这与您正在观察目录这一事实无关,它的出现只是因为您忘记了进行转义 If you have written escape(expand('<cword>'), '\\/') instead of expand('<cword>') then such error would not appear. 如果您已编写了escape(expand('<cword>'), '\\/')而不是expand('<cword>')则不会出现此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM