Emacs Apprentice – Part 1
There is no level cap in the game of Emacs-fu. There is a point, however, where you eventually reach the level of “wizard,” and as such have access to all the 9th-tier black magic (and the prowess that brings).
I’m not a wizard. Not even close. In fact, I’m like the little mage in your party who totally sucks for the first half of the game, and the only way to deal with the issue is to stick him in the back row and keep a bigger stock of healing potions.
I have, however, learned a few things as I’ve officially embarked on the path of learning Emacs. Though I know I have a long way to go, I can also see how far I’ve come, and as such I feel semi-qualified to offer some pointers to those who might be thinking of “donning the pointy hat” as well. Keep in mind that what I talk about is not the advice of an expert, and some of it may even be not optimal or downright wrong (*gasp* – misinformation in a blog?!) Alright, enough disclaimers, moving on.
Why Emacs? – This question is discussed enough all over the Internet that it probably doesn’t require my piddling 2 cents. I will merely echo Steve Yegge’s thoughts (though be warned that Steve is known for long articles, and his target audience is perhaps a bit more experienced — but still, great post), especially about the importance of not having a compulsory GUI, having a standard interface to various functionality, macros, and above all, being customizable and programmable. Oddly enough, those aren’t the things that first hit you when you start Emacs for the first time (what actually hits you is an annoying splash screen that I recommend you disable; more on that later). What first strikes you is the fact that keyboard shortcuts are required, and lots of them. This is significant.
Keyboard Shortcuts – The necessity of keyboard shortcuts is inextricably tied to a lack of dependence on a GUI. Yes, Emacs has a GUI, yes, you can use the mouse with it. But these things are not necessary, and indeed if you were basing your editor choice on graphical mousiness, you probably wouldn’t have ended up on the Emacs path. This seems like a convenient time for a side note.
SIDE NOTE: Emacs, like anything else, is really only going to work for you if you want it to. If you fight it like I fight using PHP for a project, then you will never take full advantage of it. Some people just can’t handle that period of time wherein it takes longer to do things that you could do fast before. Indeed, if increased editing speed (in a purely character-output-per-minute way) were the only benefit to using Emacs instead of Word or Eclipse, it is possible that switching would be a case of so-called “premature optimization.” For what it’s worth, I have always sorta loved that feeling of embarking on a new project, knowing that I will improve, and knowing that where I end up in the end will have advanced me farther as a person/programmer/musician/what have you. To me, temporary confusion and slowness are a natural part of learning any worthwhile thing, and after officially becoming a certified noob in enough things, I can offer my comfort: “Don’t worry, this too shall pass.”
Keyboard Shortcuts (cont’d) – The first thing to know about keyboard shortcuts in Emacs, is that everything is a function. That is, when you press the letter ‘a’, you are really just calling a function that produces the character ‘a’ on the screen. This means that keyboard commands can be “intelligent”–able to perform differently given the situation. The classic example is the Tab key. In most editors, Tab either produces a ‘tab’ character, or a certain number of spaces. This behavior doesn’t really change. In Emacs, however, frequently the Tab key is bound to a function that does something like “properly indent this line.” (A side benefit of this, is that you can press Tab anywhere in the line, not just at the beginning, and the line will be properly indented for you.) This should not be confused with vi/Vim, which have explicit modes that you enter and exit. To account for the difference in ideology, Emacs has a lot of “chords” – keyboard shortcuts that require modifier keys (generally Ctrl and Alt, which it refers to as ‘Control’ and ‘Meta’ respectively). Additionally, some commands have two steps. The exact number of commands varies depending on what you are doing, but no matter how you slice it, this is a learning curve. Here’s what I recommend:
Note: I will use the standard notation of representing commands from here on out: a capital C means Ctrl, a capital M means Meta, and a connecting dash means press-while-holding-the-modifier-key. Examples: C-p means hold Ctrl and press ‘p’. M-< means hold the Meta key (usually Alt) and press ‘<’ (shift-,). Two-step commands are separated by a space: C-x x means do Ctrl-x, then release, then press x.
- Wean yourself off the mouse, as gradually as you are comfortable. Every time you do some action with your mouse, including selecting text, make a mental note of it. Find out the way to do it using only the keyboard. (Side note: The mouse isn’t evil, and for some large selections that require precision, it is in fact faster. But as a general rule, it’s faster to use the keyboard. Not to mention the side benefit of being able to be just as effective in Emacs in the terminal over SSH or whatever.) Especially take note of occasions when you are using the toolbar; certainly anything important enough to have a big icon on a toolbar has an easily-accessible keyboard shortcut.
- Use mnemonics until muscle memory takes over. Associate “next” and “previous” with C-n and C-p, and “forward” and “backward” with C-f and C-b. ‘A’ is the beginning of the alphabet; C-a takes you to the beginning of a line. (C-z isn’t used for end-of-line, however, probably because it’s a bit awkward to type for a common operation. C-e is used instead. Think ‘e’ for ‘end.’) Note that sometimes the Meta version of a command has the opposite effect: M-v does the opposite of C-v. Come up with your own associations, but remember that eventually some aren’t going to make sense (why is ‘cancel’ C-g? Probably just because it’s common and therefore should be easy to type).
- Find a cheat-sheet, but not one that’s too huge. Don’t overwhelm yourself or make it too hard to find the shortcuts for common commands amongst extraneous ones. Worry about getting basic movement and text-manipulation commands down first.
- Don’t use the arrow keys… ever. This also goes for the other goofy keys: Home, End, PgUp, PgDown. The goal is to keep your hands on the home keys, and avoid taking them off as much as possible. This is for speed reasons, ergonomic reasons, and most of all for “low-impact typing” reasons–arm movement can distract your train of thought. In any case, the point is you have absolutely nothing to lose by avoiding these keys in Emacs, as it provides easy shortcuts for all of those movement functions, and they should be the first ones you commit to memory. (Side note: obviously if you have a crazy keyboard like this Kinesis, this may not apply, as it completely repositions the aforementioned keys to better locations. Though to be honest, I have one, and still don’t use those keys, because they still aren’t quite as good as the shortcut variants, in my humble opinion.)
- Rebind your Ctrl key. If you read the link I posted above, you already know this, but I felt I should mention it here since I just gave the spiel about keeping your hands on the home row. On most keyboards, Ctrl is horridly placed off in the nether-regions of Qwertland. CapsLock, a nearly useless key, gets a sweet spot on the home row. Ubuntu and OS X provide very easy settings in the GUI that let you remedy this grave design error, and you can read in Steve’s post linked above about how to hack Windows to do it.
- Help your brain and fingers adapt to the shortcuts. Adapting already takes a long time; why not help it along where possible? There is an excellent add-on for Firefox called Firemacs that lets you use many Emacs keyboard commands in the browser. (I may write a post devoted entirely to it at some point; it’s that good.) Many IDEs let you use Emacs keybindings as an option. Linux terminals support many of the Emacs commands, and even OS X supports a subset of them in all of its programs–both out of the box.
The .emacs File – After keyboard shortcuts, you’ll find that to make Emacs worth a darn, you’ll want to tailor it to your needs. This often means taking advantage of “modes” that other people have written. For example, Org Mode for note-taking and task management, Haskell Mode for writing Haskell code, etc. An immense amount of work has already been done for you, taking advantage of Emacs’ extensibility. But it can’t rightly just flip on every mode there is for you. The place to tell Emacs how to behave is your .emacs file, a special file that’s generally placed in your home directory. In it, you write commands in Emacs Lisp that you want Emacs to execute on start-up. Remember, Emacs does everything via functions (specifically, Emacs Lisp functions), so you can tell it to do anything you want here–from telling it to automatically go into Org Mode when you open a text file, to resizing the window to your preferred dimensions, to rebinding keys to the way you like them, to telling it to allow ‘y’ instead of ‘yes’ when it asks you a question, to telling it to save its autosave files to a specified directory… you get the picture. For this reason, a “foreign” Emacs is extremely difficult to use — but quickly swap in your personal .emacs, and you are good to go on any machine.
SIDE NOTE: When you make changes to your .emacs, you don’t have to restart Emacs to have them take effect. In fact, in theory you never need to restart Emacs. Thanks to the dynamic nature of Lisp code, you can evaluate changes and commands on the fly, by simply doing C-x C-e after the command. You can even test stuff out in the “scratch” buffer that Emacs provides for you at start-up using this same method.
- Start with copy-and-pasting. Some may not agree with me, but I think that you should be able to use Emacs in a fairly agile fashion without having to understand elisp (Emacs Lisp, the programming language of the editor). Certainly eventually it is a useful skill to develop. But for starters, you can take advantage of others’ elisp code without really knowing what’s going on. Generally, people provide the snippet of code that you need to paste into your .emacs file to get their functionality working. For example, you can get Haskell Mode up and running quite well just by pasting this into your .emacs. Generally all this sort of thing requires is a filepath edit or two.
- Comment chunks of the file. A comment in Lisp is done by typing a semicolon; the comment then continues to the end of the line. Especially if you don’t know elisp, it is very useful to put an explanatory comment before the various chunks of things you are putting in your .emacs file. This can prevent confusion later! Notice how I commented the snippet of code that sets up Haskell Mode above.
- Have a copy of your .emacs in a central location. By that I mean, keep an updated copy of the file on your webserver, or in Gmail, or in SVN or whatever. Your .emacs is what makes your Emacs yours. If you ever need to use Emacs on a different machine, or if you have to reinstall your OS or something, you’ll want a copy readily available. Keep in mind that you often can’t just use it unchanged on different machines, though; filepaths may be different.
- Be consistent in where you put your downloaded extensions. Though this isn’t strictly to do with the .emacs file, it is a good idea nonetheless, and one which I wish I’d have had earlier. If you put all your downloaded Emacs stuff (Haskell Mode, etc.) in a convenient and consistent place, then it keeps the filepaths in your .emacs consistent, and thus minimizes changes you have to make when on a different machine (especially if you keep a copy of all extensions you use in a centralized place as well). Emacs doesn’t care where you put stuff as long as you tell it where it is.
It’s officially late now, so I will close. But I have more to say. So stay tuned…