Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Press" a key from inside an interactive function

Tags:

emacs

emacs24

I need to programmatically press a key from inside of an interactive function. Here's an outline of what I have so far:

(defun answer-to-life-the-universe-and-everything ()
  (interactive)
  (insert "(* 6 7)")
  ;; Need to automagically press the RETURN key here
)

My use case: in a REPL buffer, I need frequently execute a long command. I can use the above code to create an interactive function that inserts the required string, but I still have to hit RETURN manually for the REPL to read it. Terminating the string with \n or \r won't do what I need it to.

How can I do this inside of my interactive function definition?

like image 503
YosemiteMark Avatar asked Feb 04 '26 10:02

YosemiteMark


1 Answers

A simpler way to do this is to find out what command the enter key is bound to in the REPL and then call that command in your interactive function. (To find out, go to the REPL buffer and hit C-h k <return>.)

For example, enter is bound to inferior-ess-send-input when using the R REPL via ess, so this command inserts the string and "hits enter":

(defun try-this ()
  (interactive)
  (insert "print(\"hi\")")
  (inferior-ess-send-input))
like image 52
Dan Avatar answered Feb 07 '26 10:02

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!