如何在vim中插入类似的行时最小化击键?

Laz*_*zer 7 vim keyboard-shortcuts code-snippets vim-macros

有时我需要在文件中插入一些类似的行,这些行仅在序列号上有所不同.例如,

print "func 1";
print "func 2";
print "func 3";
print "func 4";
print "func 5";
Run Code Online (Sandbox Code Playgroud)

使用vim,我最后使用[yypppp]复制粘贴第一行,然后更改最后四行.如果要插入更多行,这非常慢.

在vim中有更快的方法吗?


一个例子是:

初始状态

boot();
format();
parse();
compare();
results();
clean();
Run Code Online (Sandbox Code Playgroud)

最终状态

print "func 1";
format();
print "func 2";
parse();
print "func 3";
compare();
print "func 4";
results();
print "func 5";
clean();
Run Code Online (Sandbox Code Playgroud)

Che*_*tan 12

录制一个宏.以下是您的特定示例的工作流程:

复制粘贴第一行.然后,

qa       : Start recording macro to register a
yy       : Yank current line
p        : Paste current line in line below
/\d      : Search for start of number (you can skip this command, the next command automagically moves the cursor to the number)
C-A      : Control-A increments the number
q        : Stop recording macro
3@a      : Replay macro 3 times
Run Code Online (Sandbox Code Playgroud)

您可以用任意数字替换3以继续生成print具有递增数字的新行.

对于第二个示例,您可以添加

j        : Moves one line down
Run Code Online (Sandbox Code Playgroud)

yy命令之后,获得交替的命令行和print.

  • 你实际上不需要做`/ [0-9]\+`,VIM自动将光标移动到数字上 (3认同)