在 vim 中更改部分 CamelCase 单词

发布于 2024-10-09 02:44:29 字数 267 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-10-16 02:44:29

Vim 默认不提供 CamelCase 单词文本对象,所以我们必须
我们自己创造它。好消息是我们不需要重新发明轮子
因为插件已经存在,我们可以使用它作为基础: camelCaseMotion

这个脚本定义了动作 , w、b、e(类似于 wbe),它不是按字移动(向前/向后),而是按驼峰移动;即单词边界和大写字母。问题是它没有
定义一个类似于 iw 的文本对象来选择整个单词。那么让我们创建
it:

为了完整,文本对象需要两个定义:一个用于视觉模式,一个用于
另一种用于操作员挂起模式。这是我们的定义(添加到
.vimrc):

vmap ,i <Esc>l,bv,e
omap ,i :normal v,i<CR>

如您所见,我们使用了插件定义的 ,b,e 动作。

第一个定义允许在可视模式下输入 ,i ,它将
选择当前的“内部驼峰式单词”。 l 动作是一种解决方法,可以始终获取光标所在的单词。否则,当光标位于单词的第一个字母上时,我们会获取前一个单词)

第二个定义使用第一个
一个让 dc 等运算符与 ,i 一起使用

在我们安装插件并将定义添加到 .vimrc< /代码>
我们可以做你想做的。例如,如果我举这个例子:

DefaultHandlerWord

如果我的光标位于 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 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:

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>

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 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)

The second definition uses the first
one to let operators like d or c 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:

DefaultHandlerWord

If my cursor is on the letter n of Handler I can use:

  • ,e to go to the last letter of Handler
  • ,b to go to the first letter of Handler
  • ,w to go to the first letter of Word
  • d,i to delete Handler and get only DefaultWord
  • c,i to delete Hanlder and get a chance to replace it by another word
记忆里有你的影子 2024-10-16 02:44:29

我刚刚有同样的问题,并用这个 .vimrc 映射解决了它:

nmap W /[A-Z]<CR>

当然,如果您愿意,您可以在范围内包含数字,但您现在可以使用 W“向前搜索下一个大写字母”,而不是通过 w 常规单词。不过,在这种情况下,您可能想关闭突出显示搜索。

I just had the same question, and resolved it with this .vimrc mapping:

nmap W /[A-Z]<CR>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文