如何在 Vim 中将单词的第一个字母大写?
很长一段时间以来,我都知道可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就我个人而言,这正是我不喜欢为其创建宏的事情。没有必要用几十个这样的一次性解决方案来填充您的
.vimrc
,因为该解决方案非常自然地从标准 Vim 命令的“工具箱”中流出,您可以像第二个一样将其串在一起自然。我正在输入一个长单词:(
|
是光标的位置)。突然我意识到我忘记了“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
|
is the position of the cursor). Suddenly I realize that I forgot to capitalize theG
of "Gallicization". So, bam!, I hitESC
(which is mapped to the caps lock key on my keyboard, so it takes only a flick of the pinky), followed byb~A
, and I continue typing as if nothing happened. That errantg
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.
我发现自己经常这样做,并使用 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
您还可以使用宏
,然后在每次需要使用它时执行
@
。you can also use macro
and then do
@<register>
every time you need to use it.更改多行的大写/小写
将第一个字母从小写更改为大写,反之亦然 - 使用以下命令
您可以使用步骤 -1
突出显示要更改的所有行:
<,
>或使用ctrl+v
然后使用j
或k
步骤 -2-
使用
l
或h 将高亮调整为每行的第一个字母
步骤-3-
从大写到小写使用 gu
从小写到大写使用 gU
这适用于您要突出显示的所有字母
注意:您可以使用
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 usingctrl+v
thenj
ork
step -2-
Use
l
orh
to adjust the highlight to first letter of each linestep -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