我有幾行文字,像這樣需要一些文本移動到行的開始Vim中
text1*textX*,text2
,我想給**標記之間的文本移動的開始線,所以它看起來像這樣
textX "text1" "text2"
我該怎麼做在命令行?
我有幾行文字,像這樣需要一些文本移動到行的開始Vim中
text1*textX*,text2
,我想給**標記之間的文本移動的開始線,所以它看起來像這樣
textX "text1" "text2"
我該怎麼做在命令行?
雖然Niraj Nawanit的回答可能給你你在找什麼,這就給你你所要求的:
%s/\([^*]*\)\*\(.*\)\*,\([^[:space:]]*\)/\2 "\1" "\3"/
試試下面的命令:
s/^\(.*\)\*\(.*\)\*/\2 \1/
它會修改文本1 * textX *,文本2至textX文本1,文本2
如果有文字*你需要把它們放在裏面[*]
:%s,\v^([^ ]*)\s\+([^ ]*).*,\2 \1,g
: ................. command
% ................. whole file
s ................. replace
^ ................. begining of line
, ................. search and replace delimiter (comma exchanging by bar)
\v ................ very magic, see :h very-magic
([^ ]*) ........... group 1 (group here) everything except space
\s\+ .............. one or more spaces
([^ ]*) ........... group 2 (group here) everything except space
\s\+ .............. one or more spaces
.* ................ zero or more characters
\2 ................. back reference to group 2
\1 ................. back reference to group 1
, .................. ending replace
g .................. global
另一種方法......
在上面的示例中,我會做(正常模式):
0dt*f*p
. 0 : goes to start of line
. d : starts delete (waits for movement or text object)
. t* : moves the cursor to the first asterisk and yanks the deleted text to the unnamed register
. f* : moves the cursor to the second asterisk
. p : pastes the contents of the unnamed register immediately after the second asterisk
參考文獻:
:h f
:h t
當然,它確實需要一個在你能夠理解這兩個命令的能力之前,它們是第二性的,所以你可以在現實世界中使用它們。
我有時會發現這種方法比在Vim的命令行上輸入正則表達式更有效。超過兩行的
更多:
qzq:@=n
其中n是重複因子。
這太棒了,謝謝 – Jose187