简体   繁体   中英

emacs - set shortcut key only in major mode?

I would like to override Cl and use it to do Mx erase-buffer followed by simulating hitting RET , only when I am in m-shell-mode . Cl should be its default, recenter-top-bottom , otherwise. How do I do so?

Not sure what m-shell-mode is, but if it's a well-defined major mode , then the following should do the trick:

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

Might I suggest an alternative binding, which has the same visual effect, but keeps the buffer contents around (which can be handy).

(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)

If m-shell-mode is based on comint-mode , which is true of many modes that provide a shell to interact with another process, then you can pass the return keypress to matlab with the function comint-send-input . In that case the following code should do what you want:

(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)

The first defun makes a function that does what you want. The second is a hook function that will bind Cl to that function for the buffer that is active when the function is called. The add-hook tells emacs to run the second function whenever you start m-shell-mode . You can add further m-shell-mode customizations inside the body of my-m-shell-mode , and Emacs will run all of them each time you start the mode.

If m-shell-mode is not based on comint-mode , you need to find out what happens when you press return . From a buffer that is running the mode, type Ch k RET to find the function bound to the return key. Use that function instead of comint-send-input in the code above.

您可以将以下代码添加到m-shell-mode挂钩:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM