如何在 Vim 中将单词的第一个字母大写?

发布于 2024-09-07 08:27:48 字数 291 浏览 9 评论 0原文

很长一段时间以来,我都知道可以在 Vim 中使用 ~ 来切换字符的大小写。但是有没有办法映射一个键来将单词大写并返回到之前的位置呢?

例如:

I like to drink Coca co[l]

如果我的光标位于“l”并且我意识到我也需要将“c”大写,目前,我需要这样做:

<C-c> b ~ ll i

有没有办法映射单个键来使单词的第一个字母光标下要大写并保持光标在原来的位置吗?

For a long time I've known that I can use ~ in Vim to toggle the case of a character. But is there a way to map a key to capitalize a word and go back to the previous position?

For example:

I like to drink Coca co[l]

If my cursor is at "l" and I realize I need to make the "c" capitalize as well, currently, I need to do:

<C-c> b ~ ll i

Is there a way to map a single key to make the first letter of the word under cursor to be capitalized and keep the cursor at original position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

给不了的爱 2024-09-14 08:27:48
:nmap <whatever> m`b~``
:nmap <whatever> m`b~``
凝望流年 2024-09-14 08:27:48

就我个人而言,这正是我不喜欢为其创建宏的事情。没有必要用几十个这样的一次性解决方案来填充您的 .vimrc ,因为该解决方案非常自然地从标准 Vim 命令的“工具箱”中流出,您可以像第二个一样将其串在一起自然。

我正在输入一个长单词:(

the gallicizatio|

| 是光标的位置)。突然我意识到我忘记了“Gallicization”的G大写。所以,bam!,我按下了 ESC (它映射到键盘上的大写锁定键,因此只需轻弹一下小指),然后按下 b~A >,然后我继续打字,好像什么也没发生一样。那个错误的 g 在 Emacs 用户开始将右手移向箭头键时被大写,我已经转到了句子的其余部分。

相反,对于我有一段时间没有使用的宏,我可能需要更多时间来记住我分配给该宏的键。更好的解决方案是很好地学习重要的“核心”命令,这些命令可以根据简单的规则即时组合,产生数百万种可能的效果。

Personally, this is exactly the kind of thing I prefer not to make a macro for. There's no need to fill up your .vimrc with dozens of such one-off solutions, because the solution flows so naturally from the "toolbox" of standard Vim commands, that you can just string it together like second nature.

I'm typing a long word:

the gallicizatio|

(the | is the position of the cursor). Suddenly I realize that I forgot to capitalize the G of "Gallicization". So, bam!, I hit ESC (which is mapped to the caps lock key on my keyboard, so it takes only a flick of the pinky), followed by b~A, and I continue typing as if nothing happened. That errant g was capitalized in the time it would take an Emacs user to begin moving his right hand toward the arrow keys, and I've already moved on to the rest of the sentence.

In contrast, with a macro that I haven't used in a while, it would probably take me more time just to remember what key(s) I assigned to that macro. The better solution is to learn the important "core" commands very well, which can be combined on the fly according to simple rules, with millions of possible effects.

呆橘 2024-09-14 08:27:48

我发现自己经常这样做,并使用 EX 命令行来获取与条件匹配的多个记录。在本例中,我使用如下所示的反向引用:

:%s/\(\w\)\(\w*\)/\U\1\L\2/g

和 BOOM ,解决了特定上下文中所有单词的问题。

编辑:也看看这里,刚刚意识到这个链接有类似的答案:

使用 vim 将所选内容中每个单词的首字母大写

I find myself doing this often, and using the EX command line to got through multiple records that match a condition. In this case, I use a backreference that looks like this:

:%s/\(\w\)\(\w*\)/\U\1\L\2/g

and BOOM, problem solved across all the words that are in a certain context.

EDIT: Look here too, just realized there was this link which has similar answers:

Capitalize first letter of each word in a selection using vim

季末如歌 2024-09-14 08:27:48

您还可以使用宏

q<register> <C-c> b ~ ll i q

,然后在每次需要使用它时执行 @

you can also use macro

q<register> <C-c> b ~ ll i q

and then do @<register> every time you need to use it.

困倦 2024-09-14 08:27:48

更改多行的大写/小写

将第一个字母从小写更改为大写,反之亦然 - 使用以下命令

您可以使用步骤 -1

突出显示要更改的所有行:<,>或使用 ctrl+v 然后使用 jk

步骤 -2-

使用 lh 将高亮调整为每行的第一个字母

步骤-3-

从大写到小写使用 gu
从小写到大写使用 gU
这适用于您要突出显示的所有字母
注意:您可以使用

  • gu(上 -> 下)、
  • gU(下 -> 上)和
  • g~ 来切换大小写(上 -> 下或下 -> 上)

Change the upper/lower cases of many lines

You can change the first letter from lower to upper or vice versa using

step -1-

Highlight all the lines that you want to change using :<,> or using ctrl+v then j or k

step -2-

Use l or h to adjust the highlight to first letter of each line

step -3-

From upper cases to lower cases use gu
From lower cases to upper cases use gU
this will work for all the letters that you will highlight
Note: You can use

  • gu (upper -> lower)
  • gU (lower -> upper) and
  • g~ to switch between the cases (upper -> lower or lower -> upper)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文