vim 中的内部代码完成
有一个完成类型没有在 vim 帮助文件中列出(特别是:insert.txt),但我本能地觉得经常需要它。假设我的文件中有“Awesome”和“SuperCrazyAwesome”这两个词。我发现 Awesome 的一个实例实际上应该是 SuperCrazyAwesome,所以我跳到单词的开头,进入插入模式,然后必须输入“SuperCrazy”。
我觉得我应该能够输入“S”,创建“SCrazy”,然后只需按一个或两个完成热键即可让它找到光标左侧的内容(“S”),右侧的内容(“Crazy”) "),针对文件中的所有单词 ("/S\w*Crazy/") 进行正则表达式,并为我提供一个完整的弹出菜单选择,或者如果只有一个匹配项则进行替换。
我想为此使用实际的完成系统。存在一个使用函数的“用户定义”完成,并且在帮助中有一个很好的示例用于从给定列表进行替换。但是,我似乎无法追踪实现这一目标所需的许多细节,包括:
- 如何从 vim 函数获取文件中所有单词的列表?
- 我可以像 vim 的complete 那样列出所有缓冲区中的单词(带有文件名)吗?
- 在插入模式下如何获取光标之前/之后单词中的文本?
- 完成可以替换整个单词,而不仅仅是光标吗?
我已经花了几个小时了。我总是遇到死胡同,比如 这个,它向我介绍了\%# 用于与光标位置匹配,这似乎对我不起作用。例如,搜索 \w*\%# 仅返回我所在单词的第一个字符,无论我位于其中的哪个位置。 \%# 似乎没有锚定。
There's a completion type that isn't listed in the vim help files (notably: insert.txt), but which I instinctively feel the need for rather often. Let's say I have the words "Awesome" and "SuperCrazyAwesome" in my file. I find an instance of Awesome that should really be SuperCrazyAwesome, so I hop to the beginning of the word, enter insert mode, and then must type "SuperCrazy".
I feel I should be able to type "S", creating "SCrazy", and then simply hit a completion hotkey or two to have it find what's to the left of the cursor ("S"), what's to the right ("Crazy"), regex this against all words in the file ("/S\w*Crazy/"), and provide me with a completion popup menu of choices, or just do the replace if there's only one match.
I'd like to use the actual completion system for this. There exists a "user defined" completion which uses a function, and has a good example in the helps for replacing from a given list. However, I can't seem to track down many particulars that I'd need to make this happen, including:
- How do I get a list of all words in the file from a vim function?
- Can I list words from all buffers (with filenames), as vim's complete does?
- How do I, in insert mode, get the text in the word before/after the cursor?
- Can completion replace the entire word, and not just up to the cursor?
I've been at this for a couple of hours now. I keep hitting dead ends, like this one, which introduced me to \%# for matching with the cursor position, which doesn't seem to work for me. For instance, a search for \w*\%# returns only the first character of the word I'm on, regardless of where I'm in it. The \%# doesn't seem to anchor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然它不完全遵循您过去想要的方法,但我写了 https://github.com/mjbrownie/swapit< /a> 如果您正在寻找相关关键字,它可能会执行您的任务。如果您有数百个匹配项,那么在这种情况下它就会失败。
它主要适用于 2-10 个可能的有序匹配。
您可以定义一个列表
:SwapList Awesomes Awesome MoreAwesome SuperCrazyAwesome FullCompletelyAwesome UntrustouslyAwesome
并使用递增递减键 (c+a) (c+x) 在匹配中移动
还有一些其他循环类型插件,例如我所知道的交换词vim.org 和 github。
这里的优点是您不必使用正则表达式将单词分组在一起。
Although its not exactly following your desired method in the past I've written https://github.com/mjbrownie/swapit which might perform your task if you are looking for related keywords. It would fall down in this scenario if you have hundreds of matches.
It's mainly useful for 2-10 possible sequenced matches.
You would define a list
:SwapList awesomes Awesome MoreAwesome SuperCrazyAwesome FullyCompletelyAwesome UnbelievablyAwesome
and move through the matches with the incrementor decrementor keys (c+a) (c+x)
There are also a few other cycling type plugins like swap words that I know of on vim.org and github.
The advantage here is you don't have to group words together with regex.
几年前,当我使用第三方库时,我写了类似的东西,每个组件的每个函数都具有相当长的 CamelCasePrefixes 。但那是在 Git Hub 时代之前,我认为它是一块失落的宝石,但搜索引擎说我不是一个完整的混蛋,并将其发布到 Vim wiki。
在这里: http://vim.wikia.com/wiki/Custom_keyword_completion
只是不要问我知道“MKw”是什么意思。不知道。
这需要根据您的需求进行一些调整,因为它只查找光标处的单词,但想法就在那里。它仅适用于当前缓冲区。遍历所有缓冲区会很缓慢,因为它没有创建任何索引。出于这些目的,我会使用外部 grep。
I wrote something like that years ago when working with 3rd party libraries with rather long CamelCasePrefixes in every function different for each component. But it was in Before Git Hub era and I considered it a lost jewel, but search engine says I am not a complete ass and posted it to Vim wiki.
Here it is: http://vim.wikia.com/wiki/Custom_keyword_completion
Just do not ask me what 'MKw' means. No idea.
This will need some adaptation to your needs, as it is looking up only the word up to the cursor, but the idea is there. It works for current buffer only. Iterating through all buffers would be sluggish as it is not creating any index. For those purposes I would go with external grep.