> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/latex/zh-cn/zhi-shi-ku/049-how-can-i-define-custom-vim-macros-in-a-vimrc-file-on-overleaf.md).

# 如何在 Overleaf 上的 vimrc 文件中定义自定义 Vim 宏？

## 引言

Overleaf 提供 Vim 和 Emacs 键位绑定，可在 [项目菜单](/latex/zh-cn/zhi-shi-ku/148-using-the-overleaf-project-menu.md#keybindings)中开启。然而，由于这些键位绑定是由编辑器模拟的（也就是说，并没有真正运行的 Vim 或 Emacs），因此无法在 Overleaf 中使用 Tampermonkey 通过 Vim 的标准方法添加新的绑定。这里我们展示一些应能帮助克服这一限制的方法。

### Overleaf 关于 Tampermonkey 和 UserScripts 的政策

请注意，Overleaf 并未正式支持 UserScripts 和 Tampermonkey，且自定义脚本可能会干扰 Overleaf 的正常运行。如果您正在使用此类脚本，并且需要联系 Overleaf 获取技术支持，我们恳请您先禁用任何 Tempermonkey 脚本，并检查情况是否有所改善。如果禁用脚本并未解决您的问题，请 [联系我们](https://www.overleaf.com/contact) 寻求帮助。

## Tampermonkey 用户脚本

请注意，这是一种 *不* 官方支持的方法，且界面未来可能会发生变化，因此采用了“`UNSTABLE_editor`”命名。

```latex
// ==UserScript==
// @name         Overleaf 编辑器自定义 VIM 键位映射
// @namespace    http://tampermonkey.net/
// @version      0.1
// @match        https://www.overleaf.com/project/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener("UNSTABLE_editor:extensions", (event) => {
    const { CodeMirror, CodeMirrorVim, extensions } = event.detail;

    // 添加自定义键位绑定 - 插入模式在插入时生效
    CodeMirrorVim.Vim.map("jj", "<Esc>", "insert");
    // 正常模式在退出插入模式后生效
    CodeMirrorVim.Vim.map("h", "j", "normal");
    });
})();
```


---

# 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://ayakaleaf-pro.ayaka.space/latex/zh-cn/zhi-shi-ku/049-how-can-i-define-custom-vim-macros-in-a-vimrc-file-on-overleaf.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.
