如何将键绑定到“由以下键序列表示的函数”? [英] How do I bind a key to "the function represented by the following key sequence"?

查看:87
本文介绍了如何将键绑定到“由以下键序列表示的函数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习emacs(woohoo!),而且我一直在我的 .emacs 中非常开心。不幸的是,我不知道Lisp,所以我有基本的问题。



我已经重新映射了几个键,直到我修复肌肉记忆:

 (global-set-key(kbd< f9>)'recompile)

没关系但是我怎么能说出一个模拟按几个键的键?例如,我不知道,使< f1> 做同样的 Cu 2 Cx} (加宽一个方法是查找 Cx} 调用收缩窗口水平,并做一些lambda的事情。这当然是整洁优雅的方式(你怎么做到的)但是肯定有一种方法来定义< f1> 发送击键 Cu 2 Cx}

解决方案

对于任何长期的事情,我会推荐seh所示的方法,因为在大多数情况下,这一点自然会更加强大。当然,这需要更多的工作和技术诀窍,但这一切都是值得的:)



angus'方法就像一个缩减版的键盘宏功能这给了Emacs的名称(对于所讨论的示例,使用比宏更简单)。你应该明白宏,但是它们可以非常有用,而且对于任何更复杂的事情,动态地记录一个动画就会变得更加容易,而不是手动写出所有的个人密钥。



这是我自己写的最重要的一点的总结:

  ;;;; *键盘宏
;; Cx(或F3开始录制)
;; F3插入计数器(如果录音已经开始)
;; Cu n Cx(或F3开始录制,初始计数器值< n>
;; Cx)或F4结束记录
;; Cu n Cx)或F4结束记录,然后执行宏n -1次。
;; C-x e或F4执行最后录制的键盘宏。
;; e或F4附加e或F4按重复该宏。
;; C-u n C-x e或F4执行最后录制的键盘宏倍。
;; C-x C-k r将最后一个宏应用于该区域的每一行。
;; C-x C-k e编辑键盘宏(RET为最近)。
;; C-x C-k b设置键绑定。
;;
;;如果你发现自己使用了很多宏,你甚至可以命名为
;;供以后使用,并将其保存到您的init文件。
;; M-x name-last-kbd-macro RET(name)RET
;; M-x insert-kbd-macro RET(name)RET
;;
;;有关更多文档:
;; Ch k Cx(
;; M-:(info(emacs)Keyboard Macros)RET

如果我们玩这个问题的例子,你会看到这些东西如何结合在一起...



首先,你可以定义具有 F3的宏 Cu 2 Cx } F4



然后,您可以使用 Cx Ck 暂时将其绑定到 F1 (实际上,如果F1当前是现有键盘映射的前缀键,实际上是不正确的,因为交互键入它会简单地提示剩余部分,你可以用(global-set-key(kbd< f1>)...),但我建议坚持保留的绑定)。



如果然后使用 describe-key Ch k )来检查绑定到该密钥的内容,Emacs会显示一个(lambda) expres您可以将其复制到您的init文件,如果您愿意的话。



或者,您可以命名宏,并要求Emacs将代码插入当前缓冲区中: p>

Mx name-last-kbd-macro RET > RET



此代码与 describe-key ,但如果您评估插入的宏,您将看到等价。您也可以显示(kbd...)表达式也评估为相同的值,因此这些都只是执行相同操作的其他方法。 / p>

(您可以使用* scratch *缓冲区在表达式结束后通过移动点来评估代码,并键入 Cx Ce 以显示minibuffer中的值,或 Cj 将值插入到缓冲区中。



请注意, 插入代码使用 fset 将宏分配给符号。您可以通过执行(fset)将宏绑定到一个键,然后将该符号分配给具有(全局设置键)的键,或者您可以忽略(fset),并直接分配宏值。这当然是直接等同于安哥拉的答案。



编辑:我刚刚注意到有一个 kmacro-name-last-宏函数绑定到 Cx Ck n 其形式与 name- last-kbd-macro ,但是当使用 kmacro-bind-to-key Cx < kbd> Ck b )和 describe-key


I'm just starting to learn emacs (woohoo!) and I've been mucking around in my .emacs quite happily. Unfortunately, I don't know Lisp yet, so I'm having issues with the basics.

I've already remapped a few keys until I fix my muscle memory:

(global-set-key (kbd "<f9>") 'recompile)

That's fine. But how can I tell a key to 'simulate pressing several keys'? For instance, I don't know, make <f1> do the same as C-u 2 C-x } (widen buffer by two chars).

One way is to look up that C-x } calls shrink-window-horizontally, and do some sort of lambda thing. This is of course the neat and elegant way (how do you do this?). But surely there's a way to define <f1> to send the keystrokes C-u 2 C-x }?

解决方案

For anything long-term, I would recommend the approach shown by seh, as that will naturally be more robust in most situations. It requires a little more work and know-how, of course, but it's all worthwhile :)

angus' approach is like a cut-down version of the keyboard macros feature that gives Emacs its name (and slightly simpler to use than macros for the example in question). You should definitely be aware of macros, however -- they can be exceedingly useful, and for anything more complicated it quickly becomes far easier to record one dynamically than to write out all the individual keys manually.

Here's the summary I wrote myself of the most important bits:

;;;; * Keyboard macros
;;   C-x (          or F3  Begin recording.
;;                     F3  Insert counter (if recording has already commenced).
;;   C-u <n> C-x (  or F3  Begin recording with an initial counter value <n>.
;;   C-x )          or F4  End recording.
;;   C-u <n> C-x )  or F4  End recording, then execute the macro <n>-1 times.
;;   C-x e          or F4  Execute the last recorded keyboard macro.
;;       e          or F4  Additional e or F4 presses repeat the macro.
;;   C-u <n> C-x e  or F4  Execute the last recorded keyboard macro <n> times.
;;   C-x C-k r             Apply the last macro to each line of the region.
;;   C-x C-k e             Edit a keyboard macro (RET for most recent).
;;   C-x C-k b             Set a key-binding.
;;
;; If you find yourself using lots of macros, you can even name them
;; for later use, and save them to your init file.
;;   M-x name-last-kbd-macro RET (name) RET
;;   M-x insert-kbd-macro RET (name) RET
;;
;; For more documentation:
;;   C-h k C-x (
;;   M-: (info "(emacs) Keyboard Macros") RET

If we play with the example from the question, you'll see how some of these things tie together...

To begin with, you can define the macro with F3C-u2C-x}F4

You could then bind it temporarily to F1 with C-xC-kbF1 (actually that's not true if F1 is currently a prefix key for an existing keymap, as typing it interactively will simply prompt for the remainder. You can circumvent this in code with (global-set-key (kbd "<f1>") ...), but I would suggest sticking to the reserved bindings).

If you then use describe-key (C-hk) to examine what is bound to that key, Emacs will show you a (lambda) expression which you could copy to your init file if you so wished.

Alternatively, you could name the macro and ask Emacs to insert the code into the current buffer:

M-x name-last-kbd-macro RET (name) RET
M-x insert-kbd-macro RETRET

This code will look different to the lambda expression shown by describe-key, but if you evaluate the inserted macro, you'll see the equivalence. You can likewise show that the (kbd "...") expression also evaluates to the same value, and therefore these are all just alternative ways of doing the same thing.

(You can use the *scratch* buffer to evaluate the code by moving point after the end of the expression, and either typing C-xC-e to show the value in the minibuffer, or C-j to insert the value into the buffer).

Note that the 'inserted' code uses fset to assign the macro to a symbol. You could bind the macro to a key either by executing the (fset) and then assigning that symbol to a key with (global-set-key), or you could ignore the (fset) and simply assign the macro value directly. This, of course, is directly equivalent to angus' answer.

Edit: I've just noticed that there's a kmacro-name-last-macro function bound to C-xC-kn which is nearly identical in form to name-last-kbd-macro, but which generates the lambda expression form seen when using kmacro-bind-to-key (C-xC-kb) and describe-key.

这篇关于如何将键绑定到“由以下键序列表示的函数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆