Learn Emacs Lisp by reading Emacs Lisp
Posted: - Modified: | emacs, elispLearning Emacs Lisp can help you really tweak your Emacs environment to fit you, saving time, smoothening out frustrations, and making things easier. Reading code is an essential part of learning how to code. By reading other people’s code, you’ll not only discover interesting syntax and library features, but you’ll also start absorbing the idioms – the ways of doing things. (This is why it’s important to read good, well-formatted code.)
Where to find code
You can use C-h k
(describe-key
) to explore the code behind keystrokes, menu items, and mouse actions. If the source code is installed, you should see a hyperlink in the information window that appears. It’s a good idea to install the Emacs Lisp source code for Emacs.
If you know the name of the function you’re interested in, C-h f
(describe-function
) will link to the source code and describe the function.
You can find lots of other Emacs code snippets on http://emacswiki.org and in blog posts on http://planet.emacsen.org . gnu.emacs.sources and other Emacs-related newsgroups or mailing lists are useful, too.
How to understand code
At first glance, Emacs Lisp looks like a mess of parentheses and strange incantations. setq
? defun
? cdar
?
This is true at second glance, too.
Read through the Emacs Lisp intro manual either online or in Emacs (C-h i m Emacs Lisp Intro). You’ll probably understand very little the first few times through. This is okay. All you need to do is get a vague idea of what things are called so that you can look them up when you need to.
Now read your code again. Slightly of it should make sense.
Skim the Emacs Lisp manual, too. This is a reference, so you’ll understand even less of it as an Emacs Lisp newbie, but it’s good for picking up terms.
You don’t need to understand all of Emacs Lisp in order to take advantage of other people’s configuration snippets. You’ll learn things along the way.
Make liberal use of C-h f
to describe functions and C-h v
(describe-variable
) to investigate. You can jump to function definitions with find-function
, which is worth binding to a keystroke. For example, to map F8 to find-function
temporarily, use M-x global-set-key RET F8 find-function
.
How to step through code
Edebug is an interactive debugger for Emacs so that you can step through the code instead of guessing what the code will do. (Edebug documentation) Use M-x edebug-defun
to prepare the function you’re interested in, then run the function. Press SPC
to step through the code, e
to evaluate expressions (you can use this to find the values of numbers), h
to continue until the specified point, b
to set a breakpont, g
to execute until a breakpoint, and q
to stop debugging.
Super awesome.
How to make code your own
You can copy code to the *scratch*
buffer or another file, tweak it a little, evaluate or edebug the code, and see what changed. If you like your changes, you can:
- rename the function and add it to your configuration file,
- use advice to modify the existing function, or
- redefine the function (keeping the name as-is) and adding it to your configuration file. (This can lead to weird behaviour, so do this carefully!)
Enjoy!
Other reading:
Thanks to bl3u for the nudge to write about this!
10 comments
Raymond Zeitler
2013-03-13T14:59:32ZThank you for writing about Edebug. I had no idea it was possible to step through Emacs Lisp code. I definitely want to try it!
Raymond Zeitler
2013-03-14T17:02:07ZBTW, do you know if there is an Intro to Emacs Lisp "Tip Of The Day" mode? Sample random output: <quote>The concat function links together or unites two strings of text. The result is a string. For example:
(concat "abc" "def")
=> "abcdef"</quote>
Wang Zhiyong
2013-03-15T09:14:37ZSacha, it looks like your cold is OK. Congradulations!
I had read An Introduction to Programming in Emacs Lisp many years before, and now I have forgottem most of them. Recently I use emacs and org-mode extensively, so I need to know how to configure the .emacs file. However copying and pasting other's configurations bings many initialization problems. This makes me to think how to learn Elisp effectively. The best way for my purpose may be
C-h f and C-h v
fogus
2013-03-16T11:46:37ZAny suggestions on elisp codebases that you find particularly beautiful?
sachac
2013-03-16T14:12:18ZI enjoy reading the Org codebase because I get a lot of value out of it. ("Ooooh, we can do that? Neat!") John Wiegley's work is also nice to read.
Emmanuel Goldstein
2020-05-29T05:52:39ZHi Sacha. Could I ask you about emacs-lisp?
I want to replace newlines with one space, EXCEPT for when there are two consecutive newlines. I don't know emacs-lisp, but I came up with this although it's not working. What is it wrong?
(defun remove-newlines-in-region2 ()
"Removes all newlines in the region."
(interactive)
(save-restriction
(narrow-to-region (point) (mark))
(goto-char (point-min))
(while (search-forward "\n" nil t)
(unless (search-forward "\n\n" nil t)
(replace-match " " nil t)))))
Thanks in advance!
sachac
2020-05-29T12:32:11ZTry using looking-at instead of search-forward, and you may need to save-match-data or use delete-region. Are those enough tips to get you unstuck, or do you want more?
Emmanuel Goldstein
2020-05-29T19:16:46ZI'm pretty lost. Could you tell me the solution? :)) It would take me the whole evening, and I rather have the time for the kiddos.
sachac
2020-05-31T13:51:54ZOh, are you actually trying to unfill paragraphs? This might be easier: https://www.emacswiki.org/e... or https://www.emacswiki.org/e...
Emmanuel Goldstein
2020-05-31T17:30:46ZThanks! That did the trick!