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!
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityI would like to automate the following when emacs start:
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.
(format-time-string "%a-%d-%b-%Y %H:%M:%S")
– phs
Feb 19 '17 at 11:08
(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"))
(make-buffer-notes)
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!
Example:
(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.