2

I'm an emacs noob

—why can't I use this key

(global-set-key (kbd "M-o") 'other-window)

defined in my emacs config, in ansi-term char mode?

Are all meta keybindings simply passed raw to the terminal process?

edmqkk
  • 125
  • 3
  • 1
    Read this article: https://www.masteringemacs.org/article/mastering-key-bindings-emacs . Key bindings are much more complicated and layered than just global-set-key, and this well-written article will explain a lot of things that you didn't know you didn't know. – phils May 05 '19 at 22:00

2 Answers2

4

From my point of view you should accept Cyberbisson's most useful answer. I'll give you a formal one and the way to determine that information for yourself.

Why can't I use this key (global-set-key (kbd "M-o") 'other-window) defined in my emacs config, in ansi-term char mode?

Are all meta keybindings simply passed raw to the terminal process?

F1 works as help prefix key in term-mode. If a window of a buffer in term-mode is selected the key sequence F1 k M-o shows you the command that is bound to M-o in term char mode and the active map that binds it.

The Help-buffer display the following info:

M-o runs the command term-send-raw-meta (found in term-raw-map), which is an interactive compiled Lisp function in ‘term.el’.

It is bound to ESC [email protected], ESC P..Z, ESC \..DEL.

Almost all Meta keybindings are passed raw in term char mode. But, there is a gap between M-N and M-P. M-O is not bound by term-raw-map. If you do not define it globally it is mapped to M-o. That lower-letter key is in the range M-\ .. M-DEL and will therefore be bound to term-send-raw-meta.

But, if you bind M-O explicitly with (global-set-key (kbd "M-O") 'other-window) it will also work in term char mode.

If you wanted to stick to M-o you would need to set the binding of other-window directly in term-raw-map with the following command:

(define-key term-raw-map (kbd "M-o") 'other-window)
Tobias
  • 33,167
  • 1
  • 37
  • 77
  • 2
    I presume that weird-seeming gap is explained by C-h i g (elisp)Translation Keymaps and the usage of <ESC> O by VT100 terminals. Note also the final paragraph of that info node. – phils May 05 '19 at 21:50
  • @Tobias this post https://stackoverflow.com/a/14491568/8408220 has given me the idea to use (define-key term-raw-map (kbd "M-o") 'nil) instead. Seems to work—any reason why I shouldn't? – edmqkk May 07 '19 at 01:42
  • The standard reason not to mess with term-raw-map is that as soon as you run a program in the terminal which makes use of (in this case) M-o, you will be unable to use that feature. If you never run a program in the terminal which recognises that key, then I don't think there would be a problem. Also, nil is self-quoting (just like t), so you can remove that quote. Also, you might want to wrap that inside (with-eval-after-load "term" ...) to avoid needing to load term.el first. – phils May 08 '19 at 01:54
  • Here help https://emacs.stackexchange.com/a/77885/16006 – a_subscriber Jul 06 '23 at 13:50
2

It may be helpful for you to know that in char-mode, all C-x key sequences are mapped to C-c. You don't need to define your own key sequence for what you want, therefore, you can just do C-c C-o. As you suspected, this mode passes just about everything along to the terminal that is not prefixed.

cyberbisson
  • 907
  • 1
  • 6
  • 19