0

Emacs 27.1

I run ansi-term and very often change mode from line to char mode and visa versa.

enter image description here

I do this by mouse click. It's not very convenient. How can I do it with a key binding?

1
  • 3
    This is a very low-effort question. The answer is found very easily in the manual. You're not a newcomer (you have asked more than 250 emacs questions here), so I think it's ridiculous if you are not checking the manual at this point.
    – phils
    Jul 7, 2023 at 0:07

2 Answers 2

2

ansi-term is a terminal-emulator provided by term package.

To review default keybindings added by this package, enter M-x describe-keymap RET term-raw-map (for char mode) or term-char-map (for line mode).

Namely, term-raw-map outputs (among the other things):

C-c C-j         term-line-mode
C-x C-j         term-line-mode

term-mode-map outputs:

C-c C-k         term-char-mode

Emacs manual also mentioned these keybindings in C-h i g (emacs)Term Mode

If the defaults don't suit you, use custom keybindings. For instance:

(with-eval-after-load 'term
  (define-key term-raw-map (kbd "C-c t l") #'term-line-mode)
  (define-key term-mode-map (kbd "C-c t c") #'term-char-mode))

Some may prefer to add a keybinding to term-raw-escape-map instead of term-raw-map (e.g. see C-h f term-set-escape-char). Omit prefix key (C-h i g (elisp)Prefix Keys) in such cases, since it is added by the package implicitly.

(with-eval-after-load 'term
  (define-key term-raw-escape-map (kbd "t l") #'term-line-mode))
4
  • I try to change keybinging, but get error: Symbol's value as variable is void: term-raw-map Jul 6, 2023 at 10:52
  • (require 'term) fixes this. And I'm updating the answer with another option.
    – Y. E.
    Jul 6, 2023 at 13:27
  • Slightly better to bind to term-raw-escape-map (without the escape key prefix), as it makes the binding agnostic to the user's escape key.
    – phils
    Jul 7, 2023 at 0:01
  • @phils, thanks for the feedback. I added a remark to the answer.
    – Y. E.
    Jul 7, 2023 at 10:29
3

Emacs is self–documenting, which means that you can query for what a key or a function does and get accurate information without ever leaving Emacs. Normally you would type C-h k to run the describe-key function. This would prompt you to type a key sequence, and it would then give you the help for the command that key sequence invokes. Note that in the Emacs GUI, this works for things that you can click on as well!

Of course, in an ansi-term buffer the key bindings are a little different. Almost all control keys are sent to your shell directly, so C-h is not available (it probably backspaces). You’ll have to run the describe-key command directly with M-x. Except that M-x isn’t available either (for the same reason), you have to use C-c M-x describe-key. Then click on the button in the modeline and it will open a window showing you this information:

<mode-line> <down-mouse-1> (translated from <down-mouse-1>) at that
spot runs the command term-line-mode, which is an interactive
native-compiled Lisp function in ‘term.el’.

It is bound to C-c C-j and C-x C-j.

(term-line-mode)

Switch to line ("cooked") sub-mode of term mode.
This means that Emacs editing commands work as normally, until
you type M-x term-send-input which sends the current line to the inferior.

So there you go, the command you want to run is term-line-mode, and it is bound to both C-c C-j and C-x C-j. If you do the same while the buffer is in line mode, then the button will run term-char-mode, which is bound to C-c C-k.

1
  • 1
    Note that (if available on your keyboard) you can always use <f1> as a substitute for C-h, which is useful when in term char mode.
    – phils
    Jul 6, 2023 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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