9 Tips for the aspiring Emacs playboy

December 26th, 2007

Introduction

Man fondled by three women who love his Emacs

I consider myself a beginning Lisper. I’ve been developing my software in Emacs for 8 months now. At first, I was clumsy at it. Emacs can be difficult and daunting. The terminology is different from what I’m used to, the key bindings are different, and there are just so many commands, configurations, and modes. But I’ve persevered and I now find myself quite nimble with key bindings and structured editing. And yet there’s still more to learn.

The subject of what to learn is treated in many online tutorials and printed books. But here, now, I thought I’d share some tips I use to keep the key bindings in my head from being garbage collected.

1. Post up a cheat sheet

Create your own cheat sheet with ten key bindings you’d like to learn. No more than 10. Don’t burden yourself with a gigantic list you’ll have to search through. You won’t be able to find them quickly enough, and you’ll stop referring to the sheet.

Make it a point to use bindings from the cheat sheet while you code.

At first, your cheat sheet will have the basic commands you need to master. But it will evolve as you do . . .

2. Don’t use the mouseNo Mouse

No Mouse GraphicThe mouse is a crutch for the beginner. It will keep you from learning keyboard commands. Using the keyboard will let you edit faster than the mouse ever can.

Get rid of the tool bar (tool-bar-mode -1) and the menu bar (menu-bar-mode -1).

Don’t touch the mouse. If you find yourself using the mouse too much, open up emacs in a terminal instead of in X. When you find yourself reaching for the mouse, think of a way to use the keyboard to get to where you want. In no time, you’ll be flying around the text like a crazy rocket-powered hamster on steroids instead of some wimpy mouse.

3. Read other people’s .emacs file

Lots of people post their .emacs file for bragging/demonstration purposes. These often have great tips/insights into a configuration/mode that you didn’t know about. This is like getting a look directly into the brain of an experienced Emacs user. Cut and paste and read the comments.

4. Write down any interesting commands you discover

Woman writing emacs cheat sheetWhile perusing help, reading .emacs files, loitering in the newsgroups, or surfing the web, you’ll invariably find an Emacs command that you’ll want to remember. You won’t remember it unless you write it down. Sure, you may remember some, but not all. Keep a list of key bindings you don’t want to forget. You will not be able to find it again.

5. Update your cheat sheet

Ok, so you’ve got a cheat sheet. Now what? After a week or two of using a single, ten item cheat sheet, go through the sheet and decide which key bindings you’ve graduated from.

Woman admiring a man’s cheat sheetFor each item on you cheat sheet, ask yourself these questions:

Do you use it often enough not to forget it?

Do you want to forget it?

Will you remember it without the cheat sheet?

Do you remember what it does?

When you’ve whittled down your sheet so that it again contains only those key bindings that you want to remember but don’t, add some more from the list of interesting commands you’ve been keeping. But don’t exceed a total list size of ten!

6. Keep your key bindings standard

There are a lot of key bindings that are universally accepted as standard. When you’re still learning, you won’t know which those are, and you’ll destroy any chance you have of fluency in another Emacs window if you change them. There’s nothing worse than sitting down at a terminal and being totally lost with the commands because you’re used to rebinding most of them. You might even do something awful.

It’s like being parachuted into a foreign country where “Hello, I come in peace.” means “Your mother is ugly but she’s good in bed.” But at the same time, you might use some commands so much you want to rebind them to something more useful. It’s a dilemma but I suggest keeping as many of the original key bindings as possible, especially at first.

7. Use help

Online help is a great help (wow, really bad pun). Some help commands that you might find helpful (not again):

  • describe-key (C-h k): Asks you to strike a keystroke and describes the command it is bound to.
  • describe-bindings (C-h b): Lists all of you key bindings.
  • command-apropos (C-h a): Search all of the commands in the system, and gives you a brief description of each matching command(with its key binding).
  • view-order-manuals (C-h RET): View all help commands

Also, watch when you you type a command using M-x. If there is a key binding for it, it will tell it to you.

8. Experiment

Woman with laptop receives praise from manIn order to strengthen the human-Emacs symbiotic bond, you should experiment with different editing commands and editing modes. Most text editors only offer a couple of ways of navigating text. Here are some common ones: Arrow Keys, Home/End keys, PageUp/PageDown, and find. One of the strengths of Emacs is that you can treat the same text differently depending on what command you use. For instance, you treat it as characters with C-f and C-b. But you treat it as words with M-f and M-b. You treat it as s-exprs with C-M-f and C-M-b. Etc.

In order to know when it is best to use which command, experimentation is your best option. Try out different commands and learn their strengths and weaknesses.

9. Create keyboard macros

Keyboard macros are a great thing to learn in their own right, but they can help you learn commands like a pro. When recording your macros, you have to think: “How can I do this so that this command will work perfectly a hundred times?”. The thought process you have to go through to compose those commands will reinforce all of the possible ways to perform an action.

Popularity: 13% [?]


17 Responses to “9 Tips for the aspiring Emacs playboy”

  1. Victor on December 26, 2007 10:34 am

    That’s cool!

  2. mark on December 26, 2007 12:22 pm

    I was a little surprised that you left out ‘C-h m’ from your list of key-help commands. Because it lists the mode-specific key bindings, I use it more than any of the others you listed. It provides a quick “cheat sheet” for each of the many modes and sub-modes.

    I also recommend ‘C-h i d h’ (the info system primer) so that the many manuals available inside Emacs can be looked up quickly/easily.

  3. Chris on December 27, 2007 10:53 am

    Great tips! These are pretty much what I used to learn over the past year of using Emacs exclusively for coding.

    The only difference is that instead of writing down 10 key bindings, I would find ~5 in the Emacs manual and commit them to memory and use them as much as possible for a week or two.

    In addition to writing macros, another thing I did was to force myself to use regexps to make multiple changes. This was probable the most difficult thing I did, but it’s well worth the effort. For examples of types of things you can do see here: http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html

  4. Trey Jackson on December 27, 2007 3:33 pm

    Good tips, about the only other thing I’d recommend would be to follow my blog - which has a weekly tip for Emacs. I’d put together 150 or so in my previous job and figured I should continue now that I’m outside of there.

    I’d also expand on tip #3 by proposing that you watch other people program (do whatever) in Emacs. Many things aren’t obvious by the .emacs entry - if they’re even in there (my .emacs shrinks over time b/c I offload things into auto-loading packages).

    For example, hippie-expand (a souped up version of dabbrev-expand) is amazing, but the name doesn’t help.

    Try typing M-/ in the middle of typing a word.

    Ok, now I’ve got to add that tip to my blog….

  5. Seth on December 27, 2007 8:34 pm

    Ha ha ha…these images rule.

  6. links for 2007-12-28 « Mandarine on December 27, 2007 11:33 pm

    […] 9 Tips for the aspiring Emacs playboy (tags: blog editor humor keyboard learning tutorial) […]

  7. aadis on December 29, 2007 9:15 am

    Pretty cool stuff. I would second using query-replace-regexp, it’s very very powerful.

    And the images are fun :)

  8. gary on December 29, 2007 2:35 pm

    another useful help command i discovered on accident is [prefix] C-h, which lists all commands starting with the prefix you specify. it is particularly helpful if you keep your custom behavior bound to a specific prefix, or for verifying that newly installed modes’ keymaps don’t conflict with your global bindings.

    on forcing yourself to use query-replace-regexp, i found that brute force produced the best results:

    (define-key (current-global-map) [remap isearch-forward] ‘isearch-forward-regexp)
    (define-key (current-global-map) [remap isearch-backward] ‘isearch-backward-regexp)

  9. Golodh on January 1, 2008 12:23 pm

    *sighs*
    Why bother with Emacs at all?

    There are lots of other editors that don’t requite months of learning, don’t require crib sheets, don’t use outlandish key combinations to start all sorts of wild macros, and don’t force you to write a regular expression every time you want to do a dirt-simple search and replace.

    I’ll take a version of KDE Kate (under Windows or Linux) any time over Emacs for writing code. If you want an IDE, why not take a proper IDE (KDevelop under Linux, Eclipse or even MS Visual something under Windows)? For writing text, there is always Latex (using WinEdt under Windows or Kate under Linux) or even Open Office.

    This may look like a large burden to learn all of these environments, but it isn’t. All are structured in more or less the same way, all have menus to help you remember commands (this decreases learning time enormously according to the 80%-20% rule by which you will use 20% of a programs features about 80% of the time), and all allow you to define macros and keyboard macros if you so wish.

    Why-oh-why would one use Emacs? I don’t get it. I really don’t. Let it languish as an unquiet undead in the graveyard of digital monstrosities please.

  10. Bryguy on January 2, 2008 10:13 am

    Golodh:

    Emacs has two features that none of the editors you describe have.

    1. It’s serious about working without a mouse. I hate having to use a mouse, and there’s nothing in emacs I can’t do without a mouse because that’s the way people use it, and so there’s no need to set up keyboard macros and hope they work how you want them to; they’re already built that way.

    2. It works from a terminal. I do a significant portion of my work on remote machines, and I can fire up emacs and have a full featured browser in my terminal on basically every machine I use. I guess if you run X on your servers you could install KDE and Kate and then forward the X connection which would probably be dog slow, but why-oh-why would one do that when one could just use emacs? :)

  11. Toby Haynes on January 2, 2008 11:53 am

    *sighs*
    Why bother with Emacs at all?

    Because nothing else comes close. Kate is good enough for basic work but it’s extensibility is limited. Emacs is essentially limited only by the speed of your machine, your lisp knowledge and your imagination in what it can do.

    Emacs allows you to tailor your IDE to precisely your needs, from specific indentation styles (that might be in-house specific) to asynchronously generating a graph of Class dependencies whenever you save a java file.

    Basically, there are lots of good IDEs out there.
    I have yet to find one which compares to Emacs for productivity once you’ve made the initial investment in learning the tool’s basics. From there on in, it’s all about hitting the productivity accelerator.

    Cheers,
    Toby Haynes

  12. Where Are The Wise Men? » Blog Archive » Emacs Cheat Sheet for 2008-01-02 on January 2, 2008 1:24 pm

    […] This post suggests what could be a winning solution for me – make a Emacs cheatsheet with those items you want to embed into your brain.  Every few weeks, evaluate  what you have on there, add new items, remove non-useful things, etc.  I think will work wonderful for me!  And, you know, why not post my new cheatsheets on my blog  when I change them out? […]

  13. Anant on January 3, 2008 12:54 pm

    “It’s serious about working without a mouse”

    Why would you not want to use the mouse? The number of people who think they are fast just because they use only the keyboard are growing in alarming numbers. The stop-watch has consistently proven over the years that using a combination of the mouse and keyboard is always faster than using only the keyboard, while the keyboard users are 100% of the time sure of being faster than their keyboard+mouse counterparts.

    Well, they’re wrong.

    Nice writeup though, in emacs you probably don’t need the mouse. *sigh* Why Emacs? ;)

  14. Best of Feeds - 23 links - programming, music, photography, psychology, rails « // Internet Duct Tape on January 13, 2008 9:12 am

    […] [CODE] 9 Tips for the aspiring Emacs playboy […]

  15. Meyer Billmers on January 16, 2008 9:49 pm

    I use the mouse all the time in Emacs; and I confess to rarely writing my own elisp code. But despite that, there are just too many reasons to use Emacs to seriously consider any other editor. Just a few:

    Other editors let you set a mark. Emacs lets you set 36 marks, mnemonically named if you like, in different files if you like

    Other editors let you cut and paste. Emacs lets to copy to 36 named buffers, mnemonically named.

    How about keyboard macros — save and then reexecute the same sequence of keystroke commands, including search and relplace!

    Inferior shell — run shell commands in an Emacs window where you can do any Emacs thing to them — search, save to a file, partial command recall, you name it.

    And face it — what other editor is powerful enough to run the towers of hanoi, or Eliza the “shrink” program? I even once wrote a lunar lander game entirely in Emacs. It worked well, too, including the ability to land next to McDonalds and order lunar fries :-)

    The only reason to *not* use Emacs is because you don’t want to spend a few hours learning a few basic commands.

    Oh, and another reason: if you use any Unix variant, most shells (Csh, Tcsh, Bash, etc.) use Emacs commands to allow command line editing. So once you learn the commands, you can use them there too. Or vice versa; if you’re a Unix user, you may well know more Emacs commands than you think!

  16. plh on January 28, 2008 5:34 am

    “The stop-watch has consistently proven over the years that using a combination of the mouse and keyboard is always faster than using only the keyboard”

    Extraordinary claim. References please, Anant.

    “Why-oh-why would one use Emacs? I don’t get it. I really don’t.”

    Clearly not, Golodh. And if, after months of learning, one has still failed to find the dirt-simple ‘replace-string’ (mentioned halfway through the introductory tutorial, bound to M-% and prominently located in the “Edit” menu), a (much) simpler editor is probably the wisest choice.

  17. Yak Shaving: optimizing brain usage for code snippets « Helder’s Tech Stuff on April 12, 2008 4:24 pm

    […] this one-liner is pretty useless, but at least picking an arbitrary 5 or 6 snippets to add to your dynamic cheatsheet is better than trying to randomly memorize […]

Trackback URI | Comments RSS

Leave a Reply

Name (required)

Email (required)

Website

Speak your mind