> 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-tw/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-tw/zhi-shi-ku/148-using-the-overleaf-project-menu.md#keybindings)。不過，由於這些鍵位綁定是由編輯器模擬的（也就是說，並沒有真正執行 Vim 或 Emacs），因此無法像在 Vim 中使用標準方法那樣，透過 Tampermonkey 在 Overleaf 中新增鍵位綁定。以下我們示範一些應能幫助克服此限制的方法。

### Overleaf 對 Tampermonkey 和 UserScripts 的政策

請注意，UserScripts 和 Tampermonkey 並未獲 Overleaf 官方支援，而且自訂腳本可能會干擾 Overleaf 的正常運作。如果您正在使用此類腳本，且需要聯絡 Overleaf 尋求技術支援，我們懇請您停用任何 Tempermonkey 腳本，並檢查情況是否有所改善。若停用腳本無法解決您的問題，請 [與我們聯絡](https://www.overleaf.com/contact) 尋求協助。

## Tampermonkey UserScript

請注意，這是 *不* 一種官方支援的方法，而且介面未來可能會變更，因此採用「`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://overleaf-pro.ayaka.space/latex/zh-tw/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.
