在vim中,我知道我們可以使用~
來大寫單個字符(如this question所述),但是有沒有辦法將每個單詞的首字母大寫使用vim進行選擇?在vim中選擇每個單詞的首字母大寫
例如,如果我想從
hello world from stackoverflow
改變
Hello World From Stackoverflow
我應該怎麼辦呢vim的?
在vim中,我知道我們可以使用~
來大寫單個字符(如this question所述),但是有沒有辦法將每個單詞的首字母大寫使用vim進行選擇?在vim中選擇每個單詞的首字母大寫
例如,如果我想從
hello world from stackoverflow
改變
Hello World From Stackoverflow
我應該怎麼辦呢vim的?
您可以使用以下替代:
s/\<./\u&/g
\<
單詞的開頭匹配.
一個字\u
告訴Vim爲大寫以下字符的第一個字符匹配替換字符串(&)
&
意味着替換任何在LHS: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 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.
圖案部分有這個意思。
\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_].
結果或替換部分具有這樣的意義:
\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 i.e. "\(\)" verses "()".
Vim的提示Wiki有TwiddleCase mapping將視覺選擇切換爲小寫,大寫和標題大小寫。
如果您將TwiddleCase
函數添加到您的.vimrc
,那麼您只需在視覺上選擇所需的文本並按波浪字符~
來遍歷每個案例。
還有一個非常有用的vim-titlecase
插件此。
謝謝。 Smart Title Case也適用於Sublime Text。 https://github.com/mattstevens/sublime-titlecase –
非常感謝你,特別是對每一個細節的解釋! – keelar
@keelar。不客氣:) –
我只是必須這樣做,並使用了一個我過度重複的宏,我知道必須有更好的方法,但我從來沒有通過正則表達式。這很棒。謝謝。 –