> For the complete documentation index, see [llms.txt](https://dofy.gitbook.io/learn-vim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dofy.gitbook.io/learn-vim/zh-cn/chapter07.md).

# 第七章：Vim 中的宏

宏操作在 Vim 中（甚至任何编辑器中）属于比较复杂的操作了，如果前面的内容都已经掌 握了，那么你已经可以算是一个 Vim 高手了，所以，这位高手，我们不妨再来进阶一下吧 。

还记得[上一章](/learn-vim/zh-cn/chapter06.md)中把文本转成数组的例子吧，我们还做同样的事，不过这次 是用宏来操作。

> `14gg` 跳转到准备开始处理的起始行，按指示进行操作，先看效果后解释。

```javascript
var myArray = [
按 qa 开启宏录制，前方高能，连续按 I<单引号><Esc>A<单引号><逗号><Esc>jq7@a
我也要
我也要
我也要
我也要
我也要
我也要
我也要
];
```

OMG! 发生了什么，有没有惊出一身冷汗，之前两次块操作的结果瞬间就完成了，最后再简 单做些收尾工作，去掉最后一行的逗号，集体缩进一下，搞定！

下面来解释一下刚才的操作：

* `q` 是开启录制宏，`a` 是给这次宏的录制过程一个存储位置，可以是 0-9 或 a-z；
* 然后 `I<单引号><Esc>A<单引号><逗号><Esc>j` 是你这次录制的整个宏的操作过程，意 思就是行首插入单引号，行尾插入单引号和逗号，跳到下一行；
* 接下来的 `q` 是结束本次宏的录制；
* `@` 是唤起宏，`a` 是要唤起的宏的名字（存储位置），前面的 `7` 你应该明白吧，就 是执行 7 次。

*Tips：`@@` 再次唤起最后一次执行的宏。*

***

日常工作中频繁用到的和不怎么用得上的在这七章中应该都已经涉及到了，如果 Vim 中遇 到了什么问题，或者教程中遗漏了什么常规操作，欢迎在 [issues](https://github.com/dofy/learn-vim/blob/master/issues/README.md) 中提出 来，我会尽我所能给予回答或完善到教程中。

**再次感谢您的关注！如果爱，请分享。爱生活，爱 VIM！**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dofy.gitbook.io/learn-vim/zh-cn/chapter07.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
