无法在Emacs 24.x和Prelude中将关键字绑定到term-mode-term-home

时间:2016-07-08 12:10:19

标签: emacs key-bindings

我正在尝试正确绑定C-a中的term-mode以转到命令shell中文本的开头,而不是提示的开头。我使用prelude包,除了这些代码在其他地方与其他人一起使用。

这是我使用此term-mode代码的最后一次迭代。

(require 'term)
(require 'multi-term)

(defcustom term-unbind-key-list
  '("C-z" "C-x" "C-c" "C-h" "C-y" "<ESC>")
  "The key list that will need to be unbind."
  :type 'list
  :group 'multi-term)

(defcustom term-bind-key-alist
  '(("C-c C-c" . term-interrupt-subjob)
    ("C-c C-j" . term-line-mode)
    ("C-c C-k" . term-char-mode)
    ("C-m" . term-send-raw)
    ("C-p" . previous-line)
    ("C-n" . next-line)
    ("C-s" . isearch-forward)
    ("C-r" . isearch-backward)
    ("M-f" . term-send-forward-word)
    ("M-b" . term-send-backward-word)
    ("M-o" . term-send-backspace)
    ("M-p" . term-send-up)
    ("M-n" . term-send-down)
    ("M-M" . term-send-forward-kill-word)
    ("M-N" . term-send-backward-kill-word)
    ("M-r" . term-send-reverse-search-history)
    ("M-," . term-send-input)
    ("M-." . comint-dynamic-complete))
  "The key alist that will need to be bind.
If you do not like default setup, modify it, with (KEY . COMMAND) format."
  :type 'alist
  :group 'multi-term)

(add-hook 'term-mode-hook
          (lambda ()
            (add-to-list 'term-bind-key-alist '("A-M-[" . multi-term-prev))
            (add-to-list 'term-bind-key-alist '("A-M-]" . multi-term-next))))

(add-hook 'term-mode-hook
          (lambda ()
            (define-key term-raw-map (kbd "C-y") 'term-paste)
            (define-key term-raw-map (kbd "C-a") 'term-send-home)))

(define-key term-mode-map (kbd "C-a") 'term-send-home)

每当我尝试将绑定添加到term-mode-map和/或将其加倍term-raw-map时,我eval-buffer重新加载它。当我查询C-a的键绑定时,它始终引用crux-beginning-of-linecrux库的一部分,它是prelude包的子组件。它是一种全球性的小模式,我必须以某种方式杀死它吗?它始于term-unbind-key-list,此解决方案和其他方面的绑定,无论我尝试C-a总是被发送到难题。功能

1 个答案:

答案 0 :(得分:2)

我没有使用prelude但不知道crux,但我过去常常使用multi-term。您通常不希望在defcustom中使用init.el;您应该使用customize或常规方式来设置setqsetq-default等变量。下面,我使用add-to-list

您需要弄清楚prelude和/或crux如何设置绑定(次要模式,挂钩,define-key,...)然后解决它。它可能像向term-mode添加挂钩一样简单。

(defun jpk/term-mode-hook ()
  (local-set-key (kbd "C-a") 'term-send-raw))
(add-hook 'term-mode-hook 'jpk/term-mode-hook)

这是我原来的multi-term配置:

(with-eval-after-load "multi-term"
  (defun term-send-C-x ()
    "Type C-x in term-mode."
    (interactive "*")
    (term-send-raw-string "\C-x"))

  (dolist
      (bind '(("C-<right>"     . term-send-forward-word)
              ("C-<left>"      . term-send-backward-word)
              ("C-<backspace>" . term-send-backward-kill-word)
              ("C-<delete>"    . term-send-forward-kill-word)
              ("C-k"           . term-send-raw)
              ("C-y"           . term-send-raw)
              ("C-c C-z"       . term-stop-subjob)
              ("C-c C-x"       . term-send-C-x)
              ("C-z"           . term-stop-subjob)
              ("C-c C-y"       . term-paste)
              ;; work like urxvt tabbed
              ("<S-down>"      . multi-term)
              ("<S-left>"      . multi-term-prev)
              ("<S-right>"     . multi-term-next)
              ))
    (add-to-list 'term-bind-key-alist bind))
  )

我现在使用sane-term,其中包含以下内容:

(with-eval-after-load "term"
  (defun term-send-reverse-search-history ()
    "Search history reverse."
    (interactive)
    (term-send-raw-string "\C-r"))

  (defun term-send-esc ()
    "Send ESC in term mode."
    (interactive)
    (term-send-raw-string "\e"))

  (defun term-send-return ()
    "Use term-send-raw-string \"\C-m\" instead term-send-input.
Because term-send-input have bug that will duplicate input when you C-a and C-m in terminal."
    (interactive)
    (term-send-raw-string "\C-m"))

  (defun term-send-backward-kill-word ()
    "Backward kill word in term mode."
    (interactive)
    (term-send-raw-string "\C-w"))

  (defun term-send-forward-kill-word ()
    "Kill word in term mode."
    (interactive)
    (term-send-raw-string "\ed"))

  (defun term-send-backward-word ()
    "Move backward word in term mode."
    (interactive)
    (term-send-raw-string "\eb"))

  (defun term-send-forward-word ()
    "Move forward word in term mode."
    (interactive)
    (term-send-raw-string "\ef"))

  (defun term-send-reverse-search-history ()
    "Search history reverse."
    (interactive)
    (term-send-raw-string "\C-r"))

  (defun term-send-quote ()
    "Quote the next character in term-mode.
Similar to how `quoted-insert' works in a regular buffer."
    (interactive)
    (term-send-raw-string "\C-v"))

  (defun term-send-M-x ()
    "Type M-x in term-mode."
    (interactive)
    (term-send-raw-string "\ex"))

  (defun term-send-C-x ()
    "Type C-x in term-mode."
    (interactive "*")
    (term-send-raw-string "\C-x"))

    (dolist
      (bind '(;; from multi-term
              ("C-z" . nil)
              ("C-x" . nil)
              ("C-c" . nil)
              ("C-h" . nil)
              ("C-y" . nil)
              ("<ESC>" . nil)
              ("C-c C-c" . term-interrupt-subjob)
              ("C-c C-e" . term-send-esc)
              ("C-p" . previous-line)
              ("C-n" . next-line)
              ("C-s" . isearch-forward)
              ("C-r" . isearch-backward)
              ("C-m" . term-send-return)
              ;;("C-y" . term-paste)
              ("M-f" . term-send-forward-word)
              ("M-b" . term-send-backward-word)
              ("M-o" . term-send-backspace)
              ("M-p" . term-send-up)
              ("M-n" . term-send-down)
              ("M-M" . term-send-forward-kill-word)
              ("M-N" . term-send-backward-kill-word)
              ("<C-backspace>" . term-send-backward-kill-word)
              ("M-r" . term-send-reverse-search-history)
              ("M-," . term-send-raw)
              ("M-." . comint-dynamic-complete)

              ;; personal
              ("C-<right>" . term-send-forward-word)
              ("C-<left>" . term-send-backward-word)
              ("C-<backspace>" . term-send-backward-kill-word)
              ("C-<delete>" . term-send-forward-kill-word)
              ("C-k" . term-send-raw)
              ("C-y" . term-send-raw)
              ("C-c C-z" . term-stop-subjob)
              ("C-c C-x" . term-send-C-x)
              ("C-z" . term-stop-subjob)
              ("C-c C-y" . term-paste)
              ("C-c C-u" . universal-argument)
              ("<S-down>" . sane-term-create)
              ("<S-left>" . sane-term-prev)
              ("<S-right>" . sane-term-next)))
    (define-key term-raw-map
      (read-kbd-macro (car bind)) (cdr bind)))
    )