簡體   English   中英

emacs-僅在主要模式下設置快捷鍵?

[英]emacs - set shortcut key only in major mode?

我想覆蓋Cl並使用它做Mx erase-buffer然后僅當我處於m-shell-mode時,模擬RET擊中。 Cl應該是默認值,否則為recenter-top-bottom 我該怎么做?

不知道什么是m-shell-mode ,但是如果它是定義明確的主模式 ,那么以下方法可以解決問題:

(require 'm-shell-mode)
(define-key m-shell-mode-map (kbd "C-l") 'erase-buffer)

可能我建議使用替代綁定,該綁定具有相同的視覺效果,但保留緩沖區內容(可能很方便)。

(defun shell-clear-command (&optional a)
  "\"clear\" the screen"
  (interactive "P")
  (recenter (or a 0)))
(define-key m-shell-mode-map (kbd "C-l") 'shell-clear-command)

如果m-shell-mode是基於comint-mode ,這是許多模式,提供一個外殼來與另一個進程交互的真實,那么你可以通過return按鍵與功能的MATLAB的comint-send-input 在這種情況下,以下代碼應按您的要求執行:

(defun clear-and-return () 
  "Erases the buffer, and then passes a return to the buffer process.
Assumes the buffer is attached to a comint process."
  (interactive)
  (erase-buffer) 
  (comint-send-input))

(defun my-m-shell-mode-hook ()
  (local-set-key (kbd "C-l") 'clear-and-return))

(add-hook 'm-shell-mode-hook 'my-m-shell-mode-hook)

第一個defun會執行您想要的功能。 第二個是鈎子函數,該鈎子函數會將Cl綁定到該函數,以使該函數在調用該函數時處於活動狀態。 每當您啟動m-shell-mode時, add-hook告訴emacs運行第二個功能。 您可以在my-m-shell-mode主體中添加其他m-shell-mode定制,並且每次啟動模式時Emacs都會運行所有定制。

如果m-shell-mode不是基於comint-mode ,則需要找出在按return時會發生什么。 從運行該模式的緩沖區中,鍵入Ch k RET以查找綁定到返回鍵的函數。 使用該函數代替上面的代碼中的comint-send-input

您可以將以下代碼添加到m-shell-mode掛鈎:

(local-set-key (kbd "C-l") 'erase-buffer)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM