How to "press" enter in interactive mode in Emacs Lisp?

Viewed 48

I would like to press enter in my Elisp script when I am in a REPL window.

but (newline) just moves the cursor to the next line and not simulate the press of the Enter key on my keyboard.

Is there a function in Emacs Lisp that works in Emacs interactive mode?

1 Answers

In Emacs, depending on the mode you are, the same key may have different definitions (that is, it may be bound to different commands). The best way to simulate a key press is to get exactly which function is bound to that key. You can do that with lookup-key. This function will return a function that is bound to whatever key you look up. To execute the function, use funcall:

(funcall (lookup-key (current-local-map) (kbd "RET")))

The function current-local-map returns the key map for whatever context you are in. You can use other maps depending on what you want, such as current-global-map.

Related