Emacs Macros and Keybindings

Posted: Nov 29, 2023

Tags: emacs emacs lisp

Usually, for quick hacks, its easier to define a keyboard macro in Emacs than create a custom Emacs Lisp function. If you want that macro to persist and be easily usable via a key binding, the usual advice is to do something like:

(defalias 'seach-and-replace (kmacro "M-x replace-string C-m misspeling C-m misspelling"))
(global-set-key (kbd "C-c C-f") 'search-and-replace)

That does work, but it can end up polluting your .emacs file with a bunch of extra lines and, given the global nature of Emacs Lisp variables, provides an opportunity for a name collision. Instead, you can just write:

(global-set-key (kbd "C-c C-f") (kbd "M-x replace-string C-m misspeling C-m misspelling"))
Name: This will be displayed with your post
Email: This isn't visible to or shared with anyone except me (the site owner)
Comment: