How can I create a keyboard macro where a file myfile is opened by executing M-x openmyfile. The main part should look like

(fset 'openmyfile
   [?\C-x ?\C-f <dir of myfile> return])

but I don't want to enter the whole dir to myfile by writing every single char using ?/ ?d ?i ?r ... and so on. How to insert the dir string into the macro definition?

  • 2
    What don't you just do a defun? challenge? ;-)
    – JeanPierre
    Aug 29, 2019 at 16:21
  • 2
    I think (string-to-vector (kbd "C-x C-f /path/to/you/thing <return>")) would work. Or, simpler (read-kbd-macro "C-x C-f /path/... <return>")
    – Rorschach
    Aug 29, 2019 at 17:26
  • 3
    Don't do that. Use (defun openmyfile () "Your doc string comes here." (interactive) (find-file "<path of myfile>")) instead.
    – Tobias
    Aug 29, 2019 at 19:36
  • @JeanPierre unfortunately it’s only my close-to-zero knowledge about elisp ... Aug 29, 2019 at 19:58

1 Answer 1


As stated by @Tobias in the comments, you can create a command to open your file:

(defun open-my-file () 
   (find-file "/path/to/my/file"))

Then you can call it using M-x or binding to some key, like this:

(define-key global-map (kbd "C-c C-f") 'open-my-file)

But don't be afraid of messing with elisp, it's pretty easy because you don't actually need understand it too much to do some fun stuff.

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.