需要将一些文本移动到Vim中的一行开头

时间:2012-05-19 19:17:29

标签: vim

我有几行文字,比如

text1*textX*,text2

我希望将**标记之间的文本移动到行的开头,所以它看起来像这样

textX "text1" "text2"

如何在命令行中执行此操作?

4 个答案:

答案 0 :(得分:2)

尝试以下命令:

s/^\(.*\)\*\(.*\)\*/\2 \1/

它将text1 * textX *,text2修改为textX text1,text2

答案 1 :(得分:1)

如果有文字*你需要把它们放在[*]

里面
:%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 

答案 2 :(得分:1)

另一种方法......

在上面的例子中我会做(在正常模式下):

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是重复因子。

答案 3 :(得分:0)

虽然Niraj Nawanit的回答可能会为您提供您想要的内容,但这可以为您提供您所要求的内容:

%s/\([^*]*\)\*\(.*\)\*,\([^[:space:]]*\)/\2 "\1" "\3"/