如何绑定a-z区的键位而不影响输入?

(global-set-key "xf" #'foo) 很显然不可用,会导致x无法正常输入。

再想到用timer和zap-to-char进行删除再执行, 很好实现, 但会对undo序列造成影响。

所以请教一下,有没有对全局影响小点的,bind a-z区键位的办法

chord-key 是你需要的东西吗?

你只准备绑定 “xf” 这一个快捷键吗?如果是的话,可以:

(global-set-key [key-chord ?x ?f] #'foo)
(setq input-method-function
      #'(lambda (first-char)
          (if (and (memq first-char '(?x ?f))
                   (not (sit-for 0.05 'no-redisplay)))
              (let* ((input-method-function nil)
                     (next-char (read-event)))
                (if (and (memq next-char '(?x ?f))
                         (not (eq first-char next-char)))
                    (list 'key-chord ?x ?f)
                  (push next-char unread-command-events)
                  (list first-char)))
            (list first-char))))

通用性更广一点的 key-chord ,这个实际上是从 key-chord 精简而来(应该是这个,我也不记得是不是这个了) 用法很简单,将下面代码单独放在一个文件里 require 了之后, (key-chord-define "xf" #'foo) 即可,不支持两个键位相同以及键盘宏中使用,想用更多功能还是直接使用 key-chord 比较好。

(defvar key-chord-last-unmatched nil)

;;;###autoload
(defun key-chord-define (keys command)
  "定义由两个连续按键启动的 COMMAND.
KEYS: string 或 vector of two elements, elements: 32~126 之间的 ascii codes."
  ;; Exotic chars in a string are >255 but define-key wants 128..255 for those
  (setq input-method-function #'key-chord-input-method)
  (let ((key1 (logand 255 (aref keys 0)))
        (key2 (logand 255 (aref keys 1))))
    (global-set-key (vector 'key-chord key1 key2) command)
    (global-set-key (vector 'key-chord key2 key1) command)))

(defsubst key-chord-lookup-key (key)
  "查找 KEY 的非数字定义,为数字则返回 nil."
  (let ((res (lookup-key (current-global-map) key)))
    (if (numberp res)
        nil
      res)))

(defun key-chord-input-method (first-char)
  "FIRST-CHAR 为每次输入时字符."
  (if (and (not (eq first-char key-chord-last-unmatched))
           (key-chord-lookup-key (vector 'key-chord first-char)))
      (if (sit-for 0.05 'no-redisplay)
          (progn
            (setq key-chord-last-unmatched nil)
            (list first-char))
        ;; else input-pending-p
        (let* ((input-method-function nil)
               (next-char (read-event)))
          (if (key-chord-lookup-key (vector 'key-chord first-char next-char))
              (list 'key-chord first-char next-char)
            ;; 吐出 (read-event) 吞掉的字符?
            (setq unread-command-events (cons next-char unread-command-events))
            (when (eq first-char next-char)
              (setq key-chord-last-unmatched first-char))
            (list first-char))))
    ;; else no key-chord keymap
    (setq key-chord-last-unmatched first-char)
    (list first-char)))

(provide 'key-chord)

cool!

现在在写自己的模式编辑,想把''处理成",字母双击变成大写,以及kj作escape.我也去看看key-chord抄一抄. .

其实现在所有的按键绑定都可以用general.el来搞定。针对你的需求 general 有两个方法。

(general-def "x" (general-key-dispatch 'self-insert-command
                    :timeout 0.25
                    "f" 'foo
                    "b" 'bar)
(general-defs (general-chord "xf") 'foo
              (general-chord "fx") 'foo)

如果是doom-emacs map! 封装更好。

1 个赞