在 vim 中更改部分 CamelCase 单词
使用 vim,我发现 cw
可以非常方便地更改整个单词。 Vim 将动作命令和动作动词分开,形成了非常强大的组合。我现在必须将 DefaultHandler
更改为 ContentHandler
。我很自然地认为它是“更改为下一个大写字母”,但我找不到从一个大写字母移动到下一个大写字母的动作命令。
在这种情况下,我可以使用 ctH,但是有没有办法更改(或删除等)CamelCase 单词的第一部分,而不管接下来是哪个大写字母?
Using vim, I find cw
very handy for changing an entire word. Vim's separation of motion commands and action verbs makes for very powerful combinations. I just now had to change DefaultHandler
to ContentHandler
. I naturally thought of it as "change up to the next uppercase letter", but I couldn't find a motion command that moved from one uppercase letter to the next.
In this case, I could have used ctH
, but is there a way to change (or delete, etc) the first part of a CamelCase word regardless of which uppercase letter comes next?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Vim 默认不提供 CamelCase 单词文本对象,所以我们必须
我们自己创造它。好消息是我们不需要重新发明轮子
因为插件已经存在,我们可以使用它作为基础: camelCaseMotion
这个脚本定义了动作
, w
、、b
和、e
(类似于w
、b
、e
),它不是按字移动(向前/向后),而是按驼峰移动;即单词边界和大写字母。问题是它没有定义一个类似于 iw 的文本对象来选择整个单词。那么让我们创建
it:
为了完整,文本对象需要两个定义:一个用于视觉模式,一个用于
另一种用于操作员挂起模式。这是我们的定义(添加到
.vimrc
):如您所见,我们使用了插件定义的
,b
和,e
动作。第一个定义允许在可视模式下输入
,i
,它将选择当前的“内部驼峰式单词”。 (
l
动作是一种解决方法,可以始终获取光标所在的单词。否则,当光标位于单词的第一个字母上时,我们会获取前一个单词)第二个定义使用第一个
一个让
d
或c
等运算符与,i
一起使用在我们安装插件并将定义添加到
.vimrc< /代码>
我们可以做你想做的。例如,如果我举这个例子:
如果我的光标位于
Handler
的字母n
上,我可以使用:,e
转到最后一个Handler
的字母,b
转到Handler
的第一个字母,w
转到Handler
的第一个字母Word
d,i
删除Handler
并仅获取DefaultWord
c,i
删除Hanlder
并有机会将其替换为另一个单词Vim doesn't provides a CamelCase word text object by default, so we'll have to
create it by ourselves. The good new is that we don't need to reinvent the wheel
since a plugin already exists and we can use it as a base: camelCaseMotion
This script defines motions
,w
,,b
and,e
(similar tow
,b
,e
), which do not move word-wise (forward/backward), but Camel-wise; i.e. to word boundaries and uppercase letters. The problem is that it doesn'tdefine a text object similar to
iw
to select an entire word. So let's createit:
To be complete, a text object need two definitions: one for the visual mode and
another one for the operator pending mode. Here are our definitions (to add to
the
.vimrc
):As you can see we make a use of the
,b
and,e
motions defined by the plugin.The first definition allow to type
,i
when we are in visual mode and it willselect the current "inner camelCase word". (The
l
motion is a workaround to always get the word the cursor is on. Otherwise we get the previous one when the cursor is on the first letter of a word)The second definition uses the first
one to let operators like
d
orc
work with,i
After we installed the plugin and added the definitions to our
.vimrc
we can do what you wanted. For example if I take this example:
If my cursor is on the letter
n
ofHandler
I can use:,e
to go to the last letter ofHandler
,b
to go to the first letter ofHandler
,w
to go to the first letter ofWord
d,i
to deleteHandler
and get onlyDefaultWord
c,i
to deleteHanlder
and get a chance to replace it by another word我刚刚有同样的问题,并用这个 .vimrc 映射解决了它:
当然,如果您愿意,您可以在范围内包含数字,但您现在可以使用 W“向前搜索下一个大写字母”,而不是通过 w 常规单词。不过,在这种情况下,您可能想关闭突出显示搜索。
I just had the same question, and resolved it with this .vimrc mapping:
Of course you can include numbers in the range if you want but instead of regular words via w, you can now "search forward for next capital letter" with W. You many want to turn off highlight search in this case though.