shell - Emacs 中 char 运行模式 (ansi-term) 的键盘快捷键

标签 shell emacs keyboard

我在 Emacs 24.1 中使用 WindMove 通过键盘快捷键在多个窗口中移动:

(global-set-key (kbd "M-J") 'windmove-left)          ; move to window on the left
(global-set-key (kbd "M-L") 'windmove-right)         ; move "       "    the right
(global-set-key (kbd "M-I") 'windmove-up)            ; move "       "    above
(global-set-key (kbd "M-K") 'windmove-down);         ; move "       "    below

有没有办法将这些 WindMove 键盘快捷键与 ansi-term 上的字符运行模式关联起来?我希望避免切换到行运行模式来在窗口之间移动。一段时间后,来回输入 C-c C-kC-c C-j 进入/离开终端就会变得乏味。

最佳答案

下面的代码适合您。它使用 Emacs 建议机制,允许用户动态更改函数的行为,而无需直接修改其源代码。在本例中,它用于使 winmove-* 函数在执行其原始定义之前运行 term-char-mode。如果您还使用其他窗口选择函数,例如除了 winmove-* 之外的 other-window,您可以以相同的方式建议这些函数。引用Advising Functions有关建议机制的详情。

该代码还处理与 term-raw-map 键映射相关的问题。 term-raw-map 直到 term.el 完全加载(或者 M-xansi-term 被定义)执行)。因此,您应该将 (define-key term-raw-map ...) 形式添加到 term-load-hook Hook ,该 Hook 会在 term.el 被加载,而不是将它们放在 init 文件的顶层。

(global-set-key (kbd "M-J") 'windmove-left)
(global-set-key (kbd "M-L") 'windmove-right)
(global-set-key (kbd "M-I") 'windmove-up)
(global-set-key (kbd "M-K") 'windmove-down)

(defun ansi-term-char-mode ()
  (if (string= (buffer-name) "*ansi-term*")
    (term-char-mode)))

(defadvice windmove-left (before windmove-left-ansi-term (&optional arg))
  (ansi-term-char-mode))

(defadvice windmove-right (before windmove-right-ansi-term (&optional arg))
  (ansi-term-char-mode))

(defadvice windmove-up (before windmove-up-ansi-term (&optional arg))
  (ansi-term-char-mode))

(defadvice windmove-down (before windmove-down-ansi-term (&optional arg))
  (ansi-term-char-mode))

(add-hook 'term-load-hook
  (lambda ()
    (define-key term-raw-map (kbd "M-J") 'windmove-left)
    (ad-activate 'windmove-left)
    (define-key term-raw-map (kbd "M-L") 'windmove-right)
    (ad-activate 'windmove-right)
    (define-key term-raw-map (kbd "M-I") 'windmove-up)
    (ad-activate 'windmove-up)
    (define-key term-raw-map (kbd "M-K") 'windmove-down)
    (ad-activate 'windmove-down)))

关于shell - Emacs 中 char 运行模式 (ansi-term) 的键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12504025/

相关文章:

Emacspeak 为那些没有视力障碍的人

emacs - 在 Windows 和 WSL 之间共享文件夹

linux - 如何隔离一行文本的一部分以便将其用作变量

linux - 在子目录中搜索相同的文件并替换所有子目录中的字符串

python - 将应用程序或脚本转换为 shell 命令

ios - UIKeyboardFrameBeginUserInfoKey & UIKeyboardFrameEndUserInfoKey

wpf - 如何在 WPF 中使用键盘快捷键?

shell - 如何使用 Bash shell 命令使 npm 项目在 Windows 上工作?

emacs - 在 Emacs 中替换字符

Android OnEditorActionListener() actionId 在我单击“完成”时给出 0