代码之家  ›  专栏  ›  技术社区  ›  Aakash Goel

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

  •  7
  • Aakash Goel  · 技术社区  · 14 年前

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

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

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

    在VIM中有更快的方法可以做到这一点吗?


    例如:

    初始状态

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

    最终状态

    print "func 1";
    format();
    print "func 2";
    parse();
    print "func 3";
    compare();
    print "func 4";
    results();
    print "func 5";
    clean();
    
    3 回复  |  直到 7 年前
        1
  •  12
  •   Chetan    14 年前

    记录宏。以下是特定示例的工作流:

    复制粘贴第一行。然后,

    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 命令,以获取交替的命令行和 打印 s。

        2
  •  1
  •   Benoit    14 年前

    你有插件可以做到这一点。例如, visincr . 直观地选择数字列,然后运行 :I .

    另一种方法是记录宏。运行 qx 开始录制要注册的宏 X , yiw 把单词拉到光标下, j 往下走一行, viwp 粘贴它, CTRL 要增加新的数字, q 停止录制,然后 @x 重放寄存器内容 X .

        3
  •  0
  •   Brian Rasmussen    14 年前

    对于这种特殊情况,可以使用宏。有一篇关于如何在 this post .

    您需要更改日志中的示例,首先写出整行,然后记录一个复制行并更新计数器的宏。