为特定 Emacs 模式设置自定义键绑定
虽然我知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用以下命令:
单独为 LaTeX 模式定义一个绑定。
I use the following:
to have a bind defined for LaTeX mode alone.
要在模式下绑定键,需要等待模式加载后才能定义键。人们可能需要该模式,或者使用
eval-after-load
不要忘记
'
-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
Don't forget either
'
—eval-after-load
is not a macro, so it needs them.您需要识别该模式的键映射(例如,
LaTeX-mode-map
)并使用函数define-key
。举个例子,除了在 LaTeX 模式下激活outline-minor-mode
之外,我还有:在这种情况下,主要模式 (LaTeX) 保留键绑定,但还有一个
outline-次要模式映射
。You need to identify the key map for that mode (for example,
LaTeX-mode-map
) and use the functiondefine-key
. As an example, along with activatingoutline-minor-mode
within LaTeX mode, I have:In this case the major mode (LaTeX) holds the key binding, but there is also an
outline-minor-mode-map
.其他答案都没有满足我的需求。所以这可能对其他人有帮助。如果我处于 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
在所有其他模式下: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 runsorg-cycle
, and otherwise it runsevil-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
equalsorg-mode
, we runorg-cycle
, otherwise we runevil-first-non-blank
in all other modes: