I would like to automate the following when emacs start:

  • open a new buffer
  • launch a foo function (create-name) that writes something on the buffer and copy to clipboard a name YYY for the buffer
  • save the buffer with name YYY

Thanks for suggestions!


The earlier answers give some good help. Here are a few more hints that you may find helpful:

(generate-new-buffer "foo") will create a new buffer that is not associated with any file. This new buffer if not made current buffer so, in order to write into it, you want to do (switch-to-buffer (generate-new-buffer "foo")).

(kill-new "YYY") will not only push "YYY" on top of the kill ring, it will copy it to the clipboard.

(write-file "YYY") will save the current buffer in file YYY where YYY might be a relative or an absolute path.

(kill-buffer) will delete the current buffer.

  • My goal was to quickly create an annotation file with a timestamp... your suggestion makes things faster! Thank you!
    – lap
    Feb 19 '17 at 11:00
  • Elisp is great for generating time stamps, e.g., (format-time-string "%a-%d-%b-%Y %H:%M:%S")
    – phs
    Feb 19 '17 at 11:08
  • Using (format-time-string "%Y-%_0m-%d-%H%M") as filename in my default notes-folder I have a history of my notes for subsequent research or statistical processing.
    – lap
    Feb 19 '17 at 11:43

In my .emacs file:

(defun make-buffer-notes()
  "Make notes-file at NOTES default folder (on desktop for windows)"
  (setq notes-path "c:/default/path/for/notes/")
  (setq notes-timestamp (concatenate 'string "NOTES-" (format-time-string "%Y-%_0m-%d-%H%M" (current-time))))
  (setq notes-full-path (concatenate 'string notes-path notes-timestamp  ".org"))
  (find-file notes-full-path)
  (insert "* " notes-timestamp  " :NOTES:\b\n"))


This is exactly what Keyboard Macros are for!

Start recording C-x (
Accomplish what it is you want to do
Stop recording C-x )
Name your new macro M-x name-last-kbd-macro
Save it to init M-x insert-kbd-macro

Now you can even assign it to a keybind!

(global-set-key (kbd "C-c a") 'my-macro)

Don't worry about getting the steps when defining the macro perfect. If you make a mistake just correct it and keep going, all that matters is the end result is what you desire.

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.