在插入模式下将 Vim 中单词的第一个字符大写,并将 return 大写到同一位置

Capitalise word's first character in Vim from insert mode, and return to the same place

在写作时,我经常发现我最终会得到这样的东西(其中 ][ 是光标):

quux foo fooba][rino

我的意思是

quux foo Fooba][rino

是否可以从插入模式将 foobarino 大写为 Foobarino,并在同一个地方结束,而不擦除您可能设置的某些标记?

我知道我可以使用 bgUlea 之类的东西来移动单词的开头,大写一个字符并移动到末尾并追加,但我想从我离开的地方继续。

您可以将 ea 替换为 gi

:help gi 说:

Insert text in the same position as where Insert mode
was stopped last time in the current buffer.
This uses the |'^| mark.  It's different from "`^i"
when the mark is past the end of the line.
The position is corrected for inserted/deleted lines,
but NOT for inserted/deleted characters.
When the |:keepjumps| command modifier is used the |'^|
mark won't be changed.

根据@romainl的想法,这里有一个完整的映射,由<C-g>~触发。

" i_CTRL-G_~        Switch case of the start of the current word from insert
"                   mode.
inoremap <C-g>~ $<BS><C-\><C-n>b~gi

它首先确保在自动缩进的情况下保持当前位置(通过插入一个虚拟字符),然后离开插入模式,跳转,执行大小写切换,最后在原位置。