1

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?

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.

2

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 C-@..N, 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)
  • 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 5 at 21:50
  • @Tobias this post 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 7 at 1: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 8 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.