使用Vim宏,您可以记录操作并将它们存储在Vim寄存器中,以便在需要时执行。
基础
qa Start recording a macro in register a
q (while recording) Stop recording macro
@a Execute macro from register a
@@ Execute the last executed macros
举例
hello
vim
macros
are
awesome
想要讲上面文本都大写
qa0gU$jq
- qa starts recording a macro in the a register.
- 0 goes to beginning of the line.
- gU$ uppercases the text from your current location to the end of the line.
- j goes down one line.
- q stops recording.
然后,利用@a重复——3@a
Safety Guard
宏执行在遇到错误时自动结束。
Command Line Macro
:normal @a
:2,3 normal @a execute your macro between lines 2 and 3
跨多个文件执行宏
- :args *.txt
find all .txt files in your current directory. - :argdo g/donut/normal @a
executes the global command g/donut/normal @a on each file inside :args - :argdo update
executes update command to save each file inside :args when the buffer has been modified.
递归宏
a. chocolate donut
b. mochi donut
c. powdered sugar donut
d. plain donut
目的:切换第一个单词的大小写
qaqqa0W~j@aq
- qaq
必须从一个空寄存器开始,因为当你递归调用宏时,它将运行寄存器中的任何内容。 - qa
starts recording on register a. - 0
goes to the first character in the current line. - W
goes to the next WORD. - ~
toggles the case of the character under the cursor. - j
goes down one line. - @a
executes macro a. - q
stops recording.
Appending A Macro
qAA.<Esc>q
- qA
starts recording the macro in register A. - A.<Esc>
在行尾插入一个点(这里A是插入模式命令,不要与宏A混淆),然后退出插入模式。 - q
stops recording macro.
修改一个宏
之前寄存器a中有一个命令——0W~A.<Esc>,用于给句子首字母切换大小写以及句尾加句号
:put a 显示的是0W~A.^[
^[ is Vim's internal code representation of <Esc>
现在需要修改它,要求可以在单词“donut”(甜甜圈)之前添加单词“deep fried”
- Back to the macro, right after the toggle case operator (~), let's add the instructions to go to the end of the line ($), go back one word (b), go to the insert mode (i), type "deep fried " (don't forget the space after "fried "), and exit insert mode (<Esc>).
- 即 0W~$bideep fried <Esc>A.^[
但是,Vim不能理解<Esc>,必须为<Esc>键编写内部代码表示。
- 在插入模式下, 按下Ctrl-V加<Esc>,就可以输出^[
Ctrl-V是一个插入模式操作符,用于逐字插入下一个非数字字符
宏代码现在应该如下所示
0W~$bideep fried ^[A.^[
要将修改后的指令添加到寄存器a中,你可以像在命名寄存器中添加一个新条目一样进行操作。在行开始处,运行"ay$将被删除的文本存储在寄存器a中。
另一种方法(在命令行)
Do :let @a=", then do Ctrl-R Ctrl-R a, this will literally paste the content of register a. Finally, don't forget to close the double quotes ("). You might have something like
:let @a="0W~$bideep fried ^[A.^["
Macro Redundancy
:let @z = @a
复制寄存器a中的宏到寄存器z
Series Vs Parallel Macro
Vim can execute macros in series and parallel.
举例
import { FUNC1 } from "library1";
import { FUNC2 } from "library2";
import { FUNC3 } from "library3";
import { FUNC4 } from "library4";
import { FUNC5 } from "library5";
If you want to record a macro to lowercase all the uppercased "FUNC", this macro should work:
qa0f{gui{jq
- qa
starts recording in register a. - 0
goes to first line. - f{
finds the first instance of "{". - gui{
lowercases (gu) the text inside the bracket - text-object (i{). - j
goes down one line. - q
stops macro recording.
但是如果中间有一行断了,比如:import { FUNC1 } from "library1";
import { FUNC2 } from "library2";
import { FUNC3 } from "library3";
import foo from "bar";
import { FUNC4 } from "library4";
import { FUNC5 } from "library5";
想跳过这一行继续运行就需要——Run the macro in parallel.
- :1,$ normal @a
网友评论