Vim 中的 CamelCase 扩展类似于 Intellij Idea?
在 Intellij Idea 中,有一个功能。假设我在代码中的某处使用了变量 myCamelCase
。然后,如果我输入 mCC
并按 Ctrl-Enter 或某些此类组合键,它会扩展为 myCamelCase
。 Vim 里有类似的东西吗?
In Intellij Idea, there's a feature. Let's say I have used a variable myCamelCase
somewhere in my code. Then if I type mCC
and press Ctrl-Enter or some such key combination, it expands to myCamelCase
. Is there something similar in Vim?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,请原谅我回答两次,但由于我的第一次尝试没有抓住要点,所以我会再试一次。这比我想象的要复杂,但可能不像我做的那么复杂(!)。
现已修改为建议所有匹配的变量名称。
首先,这是一个从“myCamelCase”字符串生成“mCC”缩写的函数:
现在,这是一个采用缩写 (' mCC')并扫描当前缓冲区(从当前行向后)以查找具有此缩写的“单词”。 返回所有匹配项的列表:
接下来,这是一个自定义完成函数,它读取光标下的单词并建议上述函数返回的匹配项:
要使用它,您必须定义“completefunc”:
要使用插入模式完成,请键入 CTRL-X CTRL-U,但我通常将其映射到CTRL-L:
使用 vimrc 中的此代码,您应该会发现键入
mCC
后跟 CTRL-L 将进行预期的替换。如果没有找到匹配的扩展,则缩写保持不变。该代码并非无懈可击,但它适用于我测试的所有简单情况。希望有帮助。如果有什么需要澄清的请告诉我。
Okay, forgive me for answering twice, but since my first attempt missed the point, I'll have another go. This is more complicated than I thought, but possibly not as complicated as I have made it (!).
This is now modified to suggest all matching variable names.
First of all, here's a function to generate the 'mCC' abbreviation from the 'myCamelCase' string:
Now, here's a function that takes an abbreviation ('mCC') and scans the current buffer (backwards from the current line) for "words" that have this abbreviation. A list of all matches is returned:
Next, here's a custom-completion function that reads the word under the cursor and suggests the matches returned by the above functions:
To make use of this, you must define the "completefunc":
To use insert-mode completion, type CTRL-X CTRL-U, but I usually map this to CTRL-L:
With this code in your vimrc you should find that typing
mCC
followed by CTRL-L will make the expected replacement. If no matching expansion is found, the abbreviation is unchanged.The code isn't water-tight, but it works in all the simple cases I tested. Hope it helps. Let me know if anything needs elucidating.
Vim 中有一个名为 vim-abolish 的插件。使用映射
crc
扩展为驼峰式大小写。There is a plugin for this in Vim called vim-abolish. Use the map
crc
to expand to camel case.