使用vimselect每个单词的首字母大写
在vim中,我知道我们可以使用~
来大写单个字符(正如在这个问题中提到的那样),但是有没有办法在使用vim的select中使每个单词的首字母大写?
例如,如果我想改变
hello world from stackoverflow
至
Hello World From Stackoverflow
我应该怎么做在vim?
你可以使用下面的replace:
s/\<./\u&/g
-
\<
匹配单词的开始 -
.
匹配一个单词的第一个字符 -
\u
告诉Vim在replacestring(&)
大写下列字符 -
&
意味着replaceLHS上匹配的任何东西
:help case
说:
To turn one line into title caps, make every first letter of a word uppercase: > : s/\v<(.)(\w*)/\u\1\L\2/g
说明:
: # Enter ex command line mode. space # The space after the colon means that there is no # address range ie line,line or % for entire # file. s/pattern/result/g # The overall search and replace command uses # forward slashes. The g means to apply the # change to every thing on the line. If there # g is missing, then change just the first match # is changed.
图案部分有这个意思。
\v # Means to enter very magic mode. < # Find the beginning of a word boundary. (.) # The first () construct is a capture group. # Inside the () a single ., dot, means match any # character. (\w*) # The second () capture group contains \w*. This # means find one or more word caracters. \w* is # shorthand for [a-zA-Z0-9_].
结果或replace部分有这个含义:
\u # Means to uppercase the following character. \1 # Each () capture group is assigned a number # from 1 to 9. \1 or back slash one says use what # I captured in the first capture group. \L # Means to lowercase all the following characters. \2 # Use the second capture group
结果:
ROPER STATE PARK Roper State Park
非常神奇的模式的替代:
: % s/\<\(.\)\(\w*\)/\u\1\L\2/g # Each capture group requires a backslash to enable their meta # character meaning ie "\(\)" verses "()".
Vim Tips Wiki有一个TwiddleCase映射 ,将视觉select切换为小写,大写和标题大小写。
如果将TwiddleCase
函数添加到.vimrc
,则只需在视觉上select所需的文本并按波浪号字符~
来遍历每个大小写。
试试这个正则expression式..
s/ \w/ \u&/g
还有这个非常有用的vim-titlecase
插件。