Skip to content

Instantly share code, notes, and snippets.

@gdsfh
Created November 28, 2014 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdsfh/a87f039e5796e57e2c3b to your computer and use it in GitHub Desktop.
Save gdsfh/a87f039e5796e57e2c3b to your computer and use it in GitHub Desktop.
; create before first run:
(defun user-macro-file() "~/.emacs.d/custom-macros.el")
(load (user-macro-file))
(defun save-macro (name)
"save a macro. Take a name as argument and save the last defined macro under this name at the end of your .emacs"
(interactive "SName of the macro: ") ; ask for the name of the macro
(kmacro-name-last-macro name) ; use this name for the macro
(find-file (user-macro-file)) ; open ~/.emacs or other user init file
(goto-char (point-max)) ; go to the end of the .emacs
(newline) ; insert a newline
(insert-kbd-macro name) ; copy the macro
(newline) ; insert a newline
(save-buffer)
(switch-to-buffer nil)) ; return to the initial buffer
(defun save-macro-bind-key (name key)
"save a macro. Take a name, keybinding as arguments and save the last defined macro under this name at the end of your .emacs"
(interactive "SName of the macro: \nKKey binding: ") ; ask for the name of the macro and a keybinding
(kmacro-name-last-macro name) ; use this name for the macro
(find-file (user-macro-file)) ; open ~/.emacs or other user init file
(goto-char (point-max)) ; go to the end of the .emacs
(newline) ; insert a newline
(insert-kbd-macro name) ; copy the macro
(newline) ; insert a newline
(insert (format "(global-set-key (kbd \"%s\") '%s)"
(key-description key) name))
(newline)
(save-buffer)
(switch-to-buffer nil) ; return to the initial buffer
(global-set-key key name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment