Macros

2021-05-11  本文已影响0人  一口亅

使用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

然后,利用@a重复——3@a

Safety Guard

宏执行在遇到错误时自动结束。

Command Line Macro

:normal @a
:2,3 normal @a execute your macro between lines 2 and 3

跨多个文件执行宏

递归宏

a. chocolate donut
b. mochi donut
c. powdered sugar donut
d. plain donut
目的:切换第一个单词的大小写

qaqqa0W~j@aq

Appending A Macro

qAA.<Esc>q

修改一个宏

之前寄存器a中有一个命令——0W~A.<Esc>,用于给句子首字母切换大小写以及句尾加句号

:put a    显示的是0W~A.^[

^[ is Vim's internal code representation of <Esc>

现在需要修改它,要求可以在单词“donut”(甜甜圈)之前添加单词“deep fried”

但是,Vim不能理解<Esc>,必须为<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

但是如果中间有一行断了,比如: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.

上一篇 下一篇

猜你喜欢

热点阅读