Emacs tips for wannabe Lispers

Learning Emacs means you no longer have to depend on somebody else’s slow, bug-ridden, and bloated graphical interface. Making the switch can be painful for a while, though. The hardest thing about it is that setting things up to do what you want is too often like an Infocom adventure game. Most of the people using it don’t want to do the kind of crappy Windowsy things that my job requires of me, so I can’t always find what I want to know via Google. Even the user manual has a pretty bitter attitude towards poor schmucks like myself running on “MS-DOG.” Meh. But we’ll just focus on Lisp hacking for now. Lisp in a Box takes care of most of the annoying set up issues, so we can just start coding.

Once you’re in Emacs, C-x C-f lets you open a new file or an existing file. If you don’t know the name of the file you’re looking for you can hit the space bar to get a new window to pop up showing you the files in the current folder. Every windows application that has a graphical file dialog generally takes eight to ten seconds to get the same thing done (Argh!), so this is one more area where Emacs can lower your blood pressure. You won’t even need the “poor-man’s” file selector most of the time. As you type out your file name you can hit tab to auto-complete stuff. Be careful, though… hitting tab too soon can take you to a list for clearing up the ambiguities….

If you see a file in windows explorer that you want to look it, just drag it onto your Emacs window.

To switch between buffers, you use the C-x b. This gives you a list of all of your buffers and you can select one by typing out the first couple of letters of its file name and hitting return. This feature is much better than the standard windows Control-F6 bit. Not only do you get a list, but often you tend to go back and forth between just two files for a while. Because the buffer list is ordered by when you last used them, it defaults to the last buffer you touched. That’s cool– especially when you’re coding in lisp switch back and forth from your code file to the REPL prompt.

The scroll bars in Emacs are really crappy. At your REPL prompt, you might scroll up to look at some of the stuff you’ve previously typed in. Then when you want to enter a new command into the top level, the scroll bars freeze up just before you get to the bottom of the buffer. The answer to this is that, first of all, you should feel guilty for even touching the mouse to begin with. Use M-> to zip down there immediately without ever leaving the safe confines of your keys.

C-x C-b opens a buffer and splits the screen horizontally. If you want to split the screen vertically, type C-x 3. Word-wrap gets turned off in that last deal, so you may find it less than useful sometimes. Your code will just trail off into the ether in vertical split mode… and worse… when an error occurs from the REPL prompt, you’ll only the first little part of the message sometimes! I’m sure this can be fixed, but I keep putting off doing the necessary research. Doing C-h a to pull up the Apropos for “wrap”, “word-wrap”, and “word” doesn’t help… but it is good to know that Emacs can studlify-word whenever I need it to. (Oh, and by the way… if you only know the Windows term for a feature… you suck! Thank-you, Emacs self-documenting features, for making my day….)

If you’re editing a function in your code file and you want to make sure that commands entered at the REPL prompt pick up the change, C-c C-c will do the trick. If you’re at the REPL prompt and want to operate on the results of a previous command, *, **, *** are variables that are bound to the last three outputs. If you want to re-enter a previous command to either run it again or edit it, M-p and M-n cycle through the list of previous commands. If you’ve already typed in the first part of the line, M-p and M-n cycle through the list of commands that start with those letters! Very handy…. And of course, the tab key grants the usual auto-completion feature… even from inside a string, too. It ain’t quite intellisense, but typing in a function name will print out its name just below the command window– it will also list its arguments, but with generic names sometimes instead of the variable names you gave it in your code file.

Cutting and pasting in Emacs is one of the hardest things to get used to if you’ve been crippled by years of habitual Windows use. Highlighting text with the mouse automatically puts the text into the copy buffer. C-w cuts the highlighted region. C-y pastes it back in. (No, I’m not yanking your chain, either!) This is really hard to get used to. I get by most of the time, but sometimes I still open Notepad if I’m going to be pushing a lot of text around. I look at the additional commands for these sorts of things but never really retain them– probably the only way I’ll learn these extra motion and editing short-cuts is to use Emacs from Cygwin without any mouse at all….

Oh, and watch out for the Undo command. It doesn’t come with a redo, but instead let’s you undo your undo’s. Use a source code control like SVN and check in regularly!

Also… when you lock up the REPL because you’ve called an out-of-control recursive function, just call M-x slime to fire it back up again.

Anyways, this is most all you really need to know to code Lisp from Emacs. If you have any additional tips or tricks– especially for Windows nerds just now getting up to speed– please post them in the comments.

Advertisements

Like this:

LikeLoading...

Related

This entry was posted on June 26, 2007 at 3:48 pm and is filed under EMACS, LISP. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.