views:

1732

answers:

9

I have been using emacs for years now and I keep meaning to get a better understanding of elisp so I can customize the editor. One problem I have found is that it is a fairly daunting task to get started with.

I have a basic understanding of LISP syntax, but is there any articles out there to help someone get started? Perhaps a tutorial on the programming, or a couple of suggestions of useful and straightforward things to implement?

+7  A: 

There's a page on this Emacs Wiki full of links to various articles about coding in Elisp.

Something to implement first would a mode for some file type not supported. For example, do you work with some esoteric configuration file that you could write a mode for? Alternatively, have a go at Windows Batch files or the hosts file. I learned a quite a lot just getting comments and IP addresses different colours in my hosts file.

Dave Webb
+3  A: 

Here's a book that I've found helpful for learning practical elisp:

http://www.amazon.com/GNU-Emacs-XEmacs-CD-ROM-Linux/dp/0761524460

Bart
+29  A: 

Emacs is large and it's totally understandable to feel daunted. Likely the path of least resistance to learning how to program Emacs lisp is to figure out some tasks that you really want to get done. Once it's personal, the motivation will be much higher.

That being said, maybe you want to write a web browser... Here are some initial ideas of simple things to implement. Most are already provided (either by emacs or additional packages), but learning to do them will entail learning how to learn about Emacs lisp.


Note: Solutions to the example problems can be found in this other answer.


load-my-favorite-file

  • a function that when called loads up your favorite (hard-coded) file. for example, your .emacs file.

cycle-special-files

  • a function that when called repeatedly, cycles through a list of your favorite files (e.g. .aliases, weeklystatus, trackerlist)

count-string-matches

  • return the number of occurrences of STRING in the buffer following the point

reload-dot-emacs

  • prompt to save .emacs file (if necessary) and load the file

strip-trailing-whitespace

  • delete whitespace at end of lines in the entire buffer

insert-code-template

  • a function that inserts a template into the current buffer e.g. a template of a header file, or a .cpp file, or a perl file
  • bonus points for providing a mechanism for making it general enough to use so that it will work differently based on the appropriate mode and/or file extension
  • hints : the variable 'major-mode, association lists
  • more bonus points, hook it up so that this function is automatically called when a new (empty) file is opened

insert-checkin-template

  • a function that prompts you for the various fields for a check-in request, giving useful defaults
  • bonus points for properly indenting the list of files

string-replace

  • take three string arguments, replace all occurrences of the first by the second in the third string. Return the newly created string.

Emacs documentation is pretty complete, here are some invaluable references:

C-h i m elisp RET           (emacs lisp info page)
M-x find-function           (show the source for the given function)
C-h C-h                     (list the help topics)
C-h f interactive RET       (read documentation for this, it's what tells Emacs the defun is a command)


And here are some of the Emacs lisp routines you'll find you need to implement the above ideas:

Movement/Positional

forward-line
forward-char
goto-char
beginning-of-line
end-of-line
point
mark

Buffer related

set-buffer
current-buffer
buffer-name

Other

search-forward
match-beginning
match-end
match-string
save-excursion
y-or-n-p
file-exists-p
find-file-noselect
copy-region-as-kill

Reading The Source

Additionally, if you like a particular piece of functionality (say, indent-region, column-number-mode, rot13-region), go and look at the source (using M-x find-function). The fact that you already know what the function does will help you quite a bit in determining how it does what it does. You start small and move to bigger things as you gain confidence. Chances are you'll learn something new with each function you examine.

Trey Jackson
Actually strip-trailing-whitespace is already implemented, it's: delete-trailing-whitespace.
Łukasz
Yes, as I mentioned in the second paragraph, most of the functionality is already provided. It is simply something small enough to do pretty easily, while still being useful.
Trey Jackson
These are great! Care to publish sample solutions? Or create an intermediate set of exercises?
Eisen
Yes, sample solutions have been added as a new answer to this question, scroll down (or click on the link provided just after the listing of examples.
Trey Jackson
+3  A: 

I've found studying other peoples .emacs files and expanding my own gave me a start in elisp. As I tried expanding the complexity from simple cut&pastes and tweak to writing functions from scratch I would occasionally find come across a breakage I didn't understand and ask on #emacs or the emacs-help mailing list.

stsquad
+13  A: 

This post by Steve Yegge is a great primer.

Bart
+1  A: 

I found the book GNU Emacs Extensions, by Bob Glickstein useful.

Brad Lucas
+7  A: 

As requested, here are some "answers" to my example exercises listed above. May you find them instructive (click on the problem name to follow the link to the solution):

The code snippets were tested on Emacs 23.1.

There are obviously many possible solutions, these are not necessarily beautiful examples of emacs lisp (the string-replace has two solutions, one rather ugly).

Trey Jackson
+1  A: 

Xah's Emacs Lisp Tutorial is a very nice resource to get one started on learning and using Elisp. It begins with the basics and provides both simple and more advance examples of Elisp code.

Ray Vega
+3  A: 

Can't believe no one has mentioned this which is the official GNU tutorial for Elisp. It's a nice read.

Noufal Ibrahim
+1, and it's also available in Emacs: M-: (info "(eintr)") RET
danlei