I just installed debian 8 and emacs24. I noticed a new feature! you now can push F11 to enter fullscreen mode. I wanted to put emacs on fullscreen mode at startup, but couldn't find the command... and then I though that it would be easier tell emacs just to "push" F11 on my .emacs

Is there any command I can use so emacs will press a given sequence of keystrokes? something like

(just-push-it "\C-x" "3" "\C-x" "\C-o" "\M-x ansi-term")
  • 4
    You can find out what command is bound to f11 by using describe-key: C-h k f11.
    – Dan
    Jun 30, 2015 at 20:24
  • zomg! thanks @Dan , that worked like a charm! just for the record, the command binded to f11 was (toggle-frame-fullscreen). I guess a command that allows you to press keystrokes is not a good idea after all... someone might change the keystrokes and everything would mess up. Anyway, I can think some crazy scenarios where someone might want to force emacs to push keystrokes...
    – Bengalaa
    Jun 30, 2015 at 20:32
  • Related post: Record your sequence of key bindings as a keyboard macro and save that an an elisp snippet. Bind that elisp fn to a key. Jun 30, 2015 at 20:38
  • You have your answer but just to be clear, a keyboard macro in emacs is the same thing as a sequence of keystrokes (and also mouse events) that you can run in emacs. I have a hard time putting kmacros into my workflow but other emacs users use them all the time. Jul 1, 2015 at 13:02
  • github.com/noctuid/general.el#simulating-keypresses works for some things that macros don't
    – fread2281
    Mar 26, 2018 at 21:40

4 Answers 4

(execute-kbd-macro (read-kbd-macro "<f11>"))

As for the key sequence syntax see the edmacro-mode help (C-h f edmacro-mode). For example, it allows you to write:

(execute-kbd-macro (read-kbd-macro "C-x 3 C-x o M-x ansi-term"))

Customize user option default-frame-alist. Add (by clicking button INS) parameter fullscreen and give it a value of fullboth. Save the option changes.

  • thanks @Drew , I just put (toggle-frame-fullscreen) on my .emacs, and now I have a fullscreen emacs on startup... I was just wondering if there is a way of just pushing some keystrokes via elisp
    – Bengalaa
    Jun 30, 2015 at 21:21
  • Well yes, you can do that. Define a keyboard macro for what you want done, then use M-x name-last-kbd-macro to give it a name. Then open your init file and use M-x insert-kbd-macro and enter the macro name. That will insert code in your init file that will run the macro. E.g., <f3>...do some stuff <f4>. Then M-x name-last-kbd-macro RET foo RET. Then in your init file: M-x insert-kbd-macro RET foo RET.
    – Drew
    Jun 30, 2015 at 22:12

The other answers pretty much cover it, but you may run into a problem with read-kbd-macro where it isn't really meant for reading raw text input.

So "hello world" becomes the macro to insert "helloworld" without the space.

You could write a function that gives you a way to specify what should be read as a macro and what is already read to be executed as is like this:

(defun push-it-real-good (&rest keys)
   (apply 'vconcat
          (mapcar (lambda (k)
                     ((listp k) (apply 'append k))
                     (t (read-kbd-macro k))))

(push-it-real-good "hello world")    ;; => helloworld
(push-it-real-good '("hello world")) ;; => hello world
;; messages Hello World
(push-it-real-good "M-:" '("(message \"Hello World\")") "<return>") 

(defun just-push-it (&rest keys)
  (setq unread-command-events
        (append (apply 'vconcat (mapcar 'kbd keys)) nil)))

(just-push-it "C-x 3" "C-x o" "M-x ansi-term" "RET" "RET")

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.