> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-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 的政策

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

## Tampermonkey 用户脚本

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

```latex
// ==UserScript==
// @name         Overleaf Editor Custom VIM Keybindings
// @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://overleaf-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.
