Emacs/elisp: global-set-key bindings not taking ef

2019-08-04 02:26发布

I am trying to bind M-<up> and M-<down> to scroll-down-line and scroll-up-line respectively as indicated here: https://stackoverflow.com/a/16229080/562139.

This is what I have in my .emacs:

;; Key bindings
(global-set-key (kbd "M-g") 'goto-line)
;; Scroll line by line
(global-set-key (kbd "M-<down>") 'scroll-up-line)
(global-set-key (kbd "M-<up>") 'scroll-down-line)

Problem: The scroll key bindings are not taking effect, while the one for goto-line does.

When I run M-x scroll-down-line however, emacs prompts me and says

"you can run the command with <M-down>"

Note: When I run global-set-key (kbd "M-<down>") 'scroll-up-line) or (global-set-key (kbd "M-<up>") 'scroll-down-line) directly in the mini-buffer, the bindings take effect! However, I seem to have noticed through the corner of my eye when I do the latter, that pressing M-<up> actually sends something like ESC ESC-<up>.

I'm foxed. What gives?

Note: I am running emacs 24.3 in a terminal (via iTerm on OSX with Option key mapped to ESC+) over SSH to a RHEL5 virtual machine.)


Update

I followed the suggestion in this answer and found that pressing M-<up> results in something completely different:

ESC <up> (translated from ESC M-[ A) runs the command
scroll-down-line, which is an interactive compiled Lisp function.

It is bound to <M-up>, ESC <up>.

(scroll-down-line &optional ARG)

I'm going to try binding that key sequence to the function and check the result.

2条回答
劫难
2楼-- · 2019-08-04 03:01

Seems key got lost in translation.

Planted a forward-paragraph at openSuse that way:

(global-set-key [(meta down)] 'forward-paragraph)
查看更多
迷人小祖宗
3楼-- · 2019-08-04 03:19
  1. Try starting Emacs without your init file: emacs -Q, and see if you can reproduce the problem. I do not see the problem, with Emacs 24.3 in terminal mode.

  2. What you saw briefly was probably ESC <up>, which is equivalent to M-<up>.

  3. Did you perhaps mean to type "When I run M-x scroll-up-line (instead of down)?

  4. I suspect that you are in some mode that gives a local binding or a minor-mode binding to these keys, which overrides the global binding. To test that, try in a buffer that is in fundamental mode. If that is the case, then to override that overriding you will need to also bind the keys in that mode's keymap.

  5. If you cannot repro the problem starting from emacs -Q then bisect your init file (~/.emacs) recursively until you find the culprit code.

查看更多
登录 后发表回答