When I use emacs serial-term mode
Can I invoke F12 or other function keys as minicom's F12?

I tried below... but it doesn't work

(global-set-key [f12] nil)

Thank you in advance!


The input of an application running in a terminal consists of characters, not keys. So Minicom doesn't directly see a key F12. What it sees is an escape sequence (which is a string starting with the escape character ?\e), which it knows represents F12. See How do keyboard input and text output work? for more information.

Therefore you need to instruct Emacs to send the escape sequence for F12 when you press F12. The code of term mode has examples of how to do it for other function keys that are predefined in this way. For example, press F1 k Left in Term mode to see what the Left key does:

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

Its definition is:

(defun term-send-left  () (interactive) (term-send-raw-string "\eOD"))

Now you need to find the escape sequence to send for F12.

  • One way to do this is to use another terminal (not Emacs-based) and press Ctrl+V F12 at a shell prompt. Ctrl+V causes the next character (which is the escape character that starts the escape sequence) to be entered literally rather than interpreted as a command (to start an escape sequence). You'll see ^[[24~, where ^[ is a visual representation of the escape character.
  • Another way to do this is to run Emacs in a terminal and press Ctrl+Q F12. Here too, you'll see ^[[24~ where ^[ is a visual representation of the escape character.
  • Alternatively, you can look up how Emacs does the translation. This information is terminal-specific, but all modern terminals are based on the same standard and xterm is the de facto reference implementation of that standard. Run M-x load-library RET term/xterm RET then look up the definition of the variable xterm-function-map (f1 v xterm-function-map RET then follow the link to the source)¹.

Now that you've identified the escape sequence, bind it in term-raw-map.

(defun term-send-f12  () (interactive) (term-send-raw-string "\e[24~"))
(define-key term-raw-map [f12] 'term-send-f12)

In your init file:

(defun my-eval-after-load-term ()
  (defun term-send-f12  () (interactive) (term-send-raw-string "\e[24~"))
  (define-key term-raw-map [f12] 'term-send-f12)
(eval-after-load "term" my-eval-after-load-term)

¹ It might be nice to look this up programmatically, but there doesn't seem to be a convenient way. (where-is-internal [f12] xterm-function-map) doesn't work (it returns nil), I don't know why.

  • Thank you so much, It works well and I learned much! Feb 8 '21 at 4:55

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.