用 Vim 生成有序列表,一个应用 Vim Macro 的例子

Vim 的 Macro 宏是一个隐藏有力的武器,在合适的场景下能发挥非常出色的作战能力。本文从一个实际编辑场景出发做一次演练。

实验要求

Vim 生成一个 20 行的有序列表,像这样——

line: 1, for ifanrx testing;
...
line: 20, for ifanrx testing;

实验结果

步骤解析

格式:键位操作(补充解释说明)

  1. i0<esc> (进入 insert mode 插入模式,输入 “0”,然后按 esc 键回到 normal mode 普通模式)
  2. m1 (将当前光标位置标记为 1)
  3. qa (开始 macro 宏录制,保存为 a)
  4. o (新开一行,并进入插入模式)
  5. line: <esc> (输入 “line: " 然后按 esc 回到普通模式)
  6. m2 (将当前光标位置标记为 2)
  7. ```1`` (按下 `1 去到标记为 1 的光标位置,也就是我们刚刚输入 0 的那个地方)
  8. <ctrl>a ( Ctrl + a 将光标下的数字加 1,0 -> 1)
  9. yiw (复制光标下的整个单词)
  10. ```2`` (去到标记为 2 的位置,也就是 “line: " 的右边)
  11. p (粘贴)
  12. , for ifanrx testing;<esc> (输入 “, for ifanrx testing;” 然后按 esc 键回到普通模式)
  13. q (结束宏录制)
  14. 20@a (重复执行刚刚录制的宏)

关键知识点

标记光标位置

m{a-zA-Z}
Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command).

宏录制

q{0-9a-zA-Z”}
Record typed characters into register {0-9a-zA-Z”} (uppercase to append). The ‘q’ command is disabled while executing a register, and it doesn’t work inside a mapping and |:normal|.

References

加载评论