Capitalize first letter of each word in a selection using vim
在vim中,我知道我们可以使用
例如,如果我想从
1 | hello world from stackoverflow |
到
1 | hello world from stackoverflow |
我该怎么在Vim里做呢?
您可以使用以下替换:
1 | s/\<./\u&/g |
\< 匹配单词的开头. 匹配单词的第一个字符\u 告诉vim将替换字符串(&) 中的以下字符大写。& 是指在左舵驾驶舱上匹配的替代品。
1 2 3 | To turn one line into title caps, make every first letter of a word uppercase: > : s/\v<(.)(\w*)/\u\1\L\2/g |
号
说明:
1 2 3 4 5 6 7 8 9 10 11 | : # Enter ex command line mode. space # The space after the colon means that there is no # address range i.e. 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. |
模式部分具有此含义。
1 2 3 4 5 6 7 8 | \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_]. |
。
结果或替换部分具有以下含义:
1 2 3 4 5 6 | \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 |
结果:
1 2 | ROPER STATE PARK Roper State Park |
。
一个非常神奇的模式的替代品:
1 2 3 | : % s/\<\(.\)\(\w*\)/\u\1\L\2/g # Each capture group requires a backslash to enable their meta # character meaning i.e."\(\)" verses"()". |
。
vim tips wiki有一个twidlecase映射,它将可视选择切换为小写、大写和标题。
如果您将
试试这个regex。
1 | s/ \w/ \u&/g |
还有非常有用的