为特定 Emacs 模式设置自定义键绑定

发布于 2024-10-29 19:44:00 字数 346 浏览 3 评论 0原文

虽然我知道如何在 Emacs 中设置全局键绑定,但我发现很难通过 Google 搜索出本地(特定于次要模式)键绑定的代码。例如,我的 .emacs 中有以下代码:

;; PDFLaTeX from AucTeX
(global-set-key (kbd "C-c M-p")
        (lambda ()
          (interactive)
          (shell-command (concat "pdflatex " buffer-file-name))))

我不想全局设置它。有没有像local-set-key这样的函数?

Though I know how to set a global key-binding in Emacs, I find it hard to even Google out the code for a local (minor-mode specific) key-binding. For instance, I have this code in my .emacs:

;; PDFLaTeX from AucTeX
(global-set-key (kbd "C-c M-p")
        (lambda ()
          (interactive)
          (shell-command (concat "pdflatex " buffer-file-name))))

I don't want to set it globally. Is there a function like local-set-key?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰魂雪魄 2024-11-05 19:44:00

我使用以下命令:

(add-hook 'LaTeX-mode-hook
          (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))

单独为 LaTeX 模式定义一个绑定。

I use the following:

(add-hook 'LaTeX-mode-hook
          (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))

to have a bind defined for LaTeX mode alone.

陌伤ぢ 2024-11-05 19:44:00

要在模式下绑定键,需要等待模式加载后才能定义键。人们可能需要该模式,或者使用 eval-after-load

   (eval-after-load 'latex 
                    '(define-key LaTeX-mode-map [(tab)] 'outline-cycle))

不要忘记 ' - eval-after-load 不是宏,所以它需要它们。

To bind a key in a mode, you need to wait for the mode to be loaded before defining the key. One could require the mode, or use eval-after-load

   (eval-after-load 'latex 
                    '(define-key LaTeX-mode-map [(tab)] 'outline-cycle))

Don't forget either 'eval-after-load is not a macro, so it needs them.

落墨 2024-11-05 19:44:00

您需要识别该模式的键映射(例如,LaTeX-mode-map)并使用函数define-key。举个例子,除了在 LaTeX 模式下激活 outline-minor-mode 之外,我还有:

  (define-key LaTeX-mode-map [(tab)] 'outline-cycle))

在这种情况下,主要模式 (LaTeX) 保留键绑定,但还有一个 outline-次要模式映射

You need to identify the key map for that mode (for example, LaTeX-mode-map) and use the function define-key. As an example, along with activating outline-minor-mode within LaTeX mode, I have:

  (define-key LaTeX-mode-map [(tab)] 'outline-cycle))

In this case the major mode (LaTeX) holds the key binding, but there is also an outline-minor-mode-map.

吻安 2024-11-05 19:44:00

其他答案都没有满足我的需求。所以这可能对其他人有帮助。如果我处于 Evil 的正常模式(基本上这意味着 Emacs 中的任何地方),我希望 Tab 跳转到行首,但如果我处于 Evil 的正常模式,我希望它在组织项目状态之间循环组织模式文档。

一种选择是每当我切换缓冲区时就使用单独的绑定和不断的绑定重新绑定(因为邪恶在正常状态下只允许每个键一个绑定)。

但更有效的选择是让 Tab 运行我自己的代码,该代码根据当前缓冲区使用的主要模式运行所需的函数。因此,如果我在组织缓冲区中,此代码将运行 org-cycle,否则它将运行 evil-first-non-blank (转到第一个非空白字符就行了)。

对于使用常规非邪恶 Emacs 的人来说,我在这里使用的技术也可以通过通过 global-set-key 调用自定义函数来使用。

对于那些不了解 Emacs lisp 的人来说,“if”语句之后的第一行是 true-action,之后的行是 false-action。因此,如果major-mode等于org-mode,我们运行org-cycle,否则我们运行evil-first-non-blank 在所有其他模式下:

  (defun my/tab-jump-or-org-cycle ()
    "jumps to beginning of line in all modes except org mode, where it cycles"
    (interactive)
    (if (equal major-mode 'org-mode)
        (org-cycle)
      (evil-first-non-blank))
    )
  (define-key evil-normal-state-map (kbd "<tab>") 'my/tab-jump-or-org-cycle)

None of the other answers satisfied my needs. So this may help other people. I wanted Tab to jump to the beginning of the line if I'm in Evil's normal mode (basically this means everywhere in Emacs), but I instead wanted it to cycle between org item states if I am in an org-mode document.

One option was to mess around with separate bindings and constant binding-rebinding whenever I switched buffers (because evil allows only one binding per key in its normal state).

But a more efficient option was to make Tab run my own code which runs the required function based on which major mode the current buffer uses. So if I am in a org buffer, this code runs org-cycle, and otherwise it runs evil-first-non-blank (go to the first non-whitespace character on the line).

The technique I used here can also be used by calling your custom function via global-set-key instead, for people who use regular non-evil Emacs.

For those who don't know Emacs lisp, the first line after the "if" statement is the true-action, and the line after that is the false-action. So if major-mode equals org-mode, we run org-cycle, otherwise we run evil-first-non-blank in all other modes:

  (defun my/tab-jump-or-org-cycle ()
    "jumps to beginning of line in all modes except org mode, where it cycles"
    (interactive)
    (if (equal major-mode 'org-mode)
        (org-cycle)
      (evil-first-non-blank))
    )
  (define-key evil-normal-state-map (kbd "<tab>") 'my/tab-jump-or-org-cycle)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文