在 vim 中更改 CamelCase 单词的部分 [英] Changing parts of CamelCase words in vim
问题描述
使用 vim,我发现 cw
非常方便地更改整个单词.Vim 将动作命令和动作动词分开,形成了非常强大的组合.我刚才不得不将 DefaultHandler
更改为 ContentHandler
.我很自然地认为它是改变到下一个大写字母",但我找不到从一个大写字母移动到下一个的运动命令.
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.
在这种情况下,我可以使用 ctH
,但是有没有办法更改(或删除等)CamelCase 单词的第一部分,而不管接下来是哪个大写字母?>
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?
推荐答案
Vim 默认不提供 CamelCase word 文本对象,所以我们必须由我们自己创建.好消息是我们不需要重新发明轮子因为插件已经存在,我们可以将其用作基础:camelCaseMotion
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
这个脚本定义了 ,w
, ,b
和 ,e
(类似于 w
, b
, e
),它们不是逐字移动(向前/向后),而是逐字移动;即单词边界和大写字母.问题是它没有定义一个类似于 iw
的文本对象来选择整个单词.所以让我们创建它:
This script defines motions ,w
, ,b
and ,e
(similar to w
, 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't
define a text object similar to iw
to select an entire word. So let's create
it:
为了完整起见,一个文本对象需要两个定义:一个用于视觉模式,一个用于另一个用于操作员挂起模式.这是我们的定义(添加到.vimrc
):
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
):
vmap ,i <Esc>l,bv,e
omap ,i :normal v,i<CR>
如您所见,我们使用了插件定义的 ,b
和 ,e
动作.
As you can see we make a use of the ,b
and ,e
motions defined by the plugin.
第一个定义允许在可视模式下输入 ,i
选择当前的内驼峰词".(l
动作是一种解决方法,始终获取光标所在的单词.否则,当光标位于单词的第一个字母上时,我们将获取前一个单词)
The first definition allow to type ,i
when we are in visual mode and it will
select 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)
第二个定义使用第一个一种让 d
或 c
等运算符与 ,i
The second definition uses the first
one to let operators like d
or c
work with ,i
在我们安装插件并将定义添加到我们的 .vimrc
之后我们可以做你想做的.例如,如果我拿这个例子:
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:
DefaultHandlerWord
如果我的光标在 Handler
的字母 n
上,我可以使用:
If my cursor is on the letter n
of Handler
I can use:
,e
转到Handler
的最后一个字母,b
转到Handler
的第一个字母,w
转到Word
的第一个字母d,i
删除Handler
,只得到DefaultWord
c,i
删除Hanlder
并有机会用另一个词代替
,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
这篇关于在 vim 中更改 CamelCase 单词的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!