vim - 在 vim 中插入类似行时,如何最大限度地减少击键次数?

标签 vim keyboard-shortcuts code-snippets vim-macros

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

print "func 1";
print "func 2";
print "func 3";
print "func 4";
print "func 5";

使用 vim ,我最终使用 [yypppp] 复制粘贴第一行,然后更改最后四行。如果您要插入更多行,这真的很慢。

在vim中有没有更快的方法来做到这一点?

这方面的一个例子是:

Initial state


boot();
format();
parse();
compare();
results();
clean();

Final state


print "func 1";
format();
print "func 2";
parse();
print "func 3";
compare();
print "func 4";
results();
print "func 5";
clean();

最佳答案

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

复制粘贴第一行。然后,

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

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

对于您的第二个示例,您只需添加
j        : Moves one line down

yy命令,以获得交替的命令行和 print的。

关于vim - 在 vim 中插入类似行时,如何最大限度地减少击键次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984967/

相关文章:

visual-studio-2008 - Visual Studio 2008 : make Ctrl + K, Ctrl + N(下一个书签)留在同一个文件中

vim - 向后删除直到Vim中的字符

visual-studio - 如何在 Visual Studio 中删除导入的代码段?

bash - Atom Snippets 不适用于 Shell 脚本

regex - 无法将两个正则表达式合并为一个以生成在 VSCode 中使用的代码片段

vim - 如何将命令应用于 Vim 中所选内容中的每一行并替换为结果(如电子表格)?

python - 试图让 vim 与 python 一起工作

vim - 在 vim 中按字母顺序排列给定范围的行

vim - 在 Vim 中修复退格键

intellij-idea - 在 IntelliJ IDEA 编辑器中删除到行尾