如何在连续的行中为我的 vi 编辑器添加前缀?

How do I add a prefix to my vi editor in consecutive lines ?

我的输入:

Test1.txt 
a
a
b
b
c
c

预期结果应该是

chown user:user a

chmod 755 a

chown user:user b

chmod 755 b

chown user:user c

chmod 755 c

请建议在单个文档中处理超过 10k 行的最佳方法。 基本上为同一路径添加 2 个不同的前缀命令(一个接一个地重复两次)

提前致谢

使用唯一路径列表更容易,因此首先删除每隔一行:

:g/^/+d

(取自this answer

然后用你想要的命令替换每一行:

:%s/.*/chown user:user &\rchmod 755 &/

:%                                     run this command on the entire buffer:
  s                                    replace
   /.*/                                an entire line with
       chown user:user                 (literal "chown user:user ")
                       &               the entirety of the match
                        \r             newline
                          chmod 755    (literal "chmod 755 ")
                                    &  the entirety of the match again
                                     / (end the regex)

&\r 记录在 :help sub-replace-special 中。)

如果你的文件是这样的

a
a
b
b
c
c

你可以使用 vi 命令

:%s/^\(.*\)\n\(.*\)/chown user:\rchmod 755 /g

如果整个文件都遵循该格式。

使用分组。 \n 匹配新行,\r 插入新行。

或者如果包含文件名并且您的文件类似于

Test1.txt 
a
a
b
b
c
c

使用

:2,$s/^\(.*\)\n\(.*\)/chown user:\rchmod 755 /g