How to Learn Emacs

So you're taking the plunge: you're going to learn Emacs.

First, the good news: you can be productive on the first day. Emacs (unlike vim) isn't something where you're incapacitated until you learn basic commands.

While you can get a leg up using this cheat sheet, you'll also be fine using the arrow keys until you learn the keystrokes.

Emacs Commands

On your first day, though, this isn't something you need to ace.

Get used to moving around at your own pace. It's okay if you're a little slow.

You can still code productively even without total Emacs mastery, at this point.

rock star

Pretty soon, this could be you.

The next thing you should concentrate on is syntax highlighting.

It may be that the language you're using already has it, or you're writing plain text and don't need it, so you can instantly check this off.

If you're not so lucky, you'll need to install the matching 'web mode' from the Emacs package repository.

Basically the process is:

This process is also covered in other online tutorials, but that's the general method.

You should be familiar with the m-x command, because you'll be using that a lot in the future, too.

Emacs Configuration

In the beginning, a lot of your 'learning' of Emacs is going to center around the configuration file.

That's located at ~/.emacs.

If you want to install custom packages to enable custom functionality, go ahead. In the end, this is a highly individual decision that comes down to your personal workflow.

For myself, I have some functions I find useful and some packages installed, but I wouldn't call it a crazy amount.

I have some functions in my .emacs file that I wrote myself, and while I use very few of them today, the learning process helped me to learn Lisp, and knowing another computer language is always a useful skill to have in your toolbox.

I'm even using one now, when writing this blog post: a function that lets me write a few sentences in plain English, and then wraps them in paragraph tags when I select the region and tell Emacs to do it.

For individual lines, I have a simpler function, shown below so you can see Emacs Lisp in it's plain form isn't that intimidating.

emacs lisp

The thing about Lisp, including Emacs Lisp, is: everything is enclosed in parenthesis. You'll get used to it.

Try following visually where the opening and closing parenthesis are, if you're confused in a code section; I know that's helped me.

Abbrev Defs

There's also a file called abbrev_defs, where you put abbreviation definitions.

This is even easier to understand than a config file, and is something you can 'master' pretty much instantly: there's one way to define abbreviations, and you vary that command over and over with new content and replacements to get the files you want.

I preface my commands with an 8, and one of the commands I use the most looks 8eu, which expands to "cd ~/Example/Update" so I can press return in the command line to put me in that frequently used directory. (I actually had to amend the command here, because I couldn't include it in its abbreviated form - I'm writing this post in Emacs, and Emacs will do as its told and insert the full command unless I fudge it).

I use that command in the shell, which brings me to my next topic.

The Shell(s)

Emacs has 2 shells: the regular shell and Emacs shell. I use them both; both serve a purpose. Most of them though, I'm using the Emacs shell. a unix shell

First of all, the shell in Emacs is a 'gift' I encourage everyone to use.

In vim, when you run a command, you often have to pipe that into | less, so you can see the screen.

In Emacs, because everything is staying in Emacs, you can just scroll up. It's as easy as that.

The ability to move around, without limitations, in the shell, and scroll up and down, and even copy commands from the shell, is something I use all the time. All. The. Time.

This is a major feature of the Emacs advantage. I strongly encourage you to use it.

There's the regular shell, for things that require the regular shell - say, some commands that won't work anywhere else.

For everything else there's the Emacs shell.

I use it frequently, for all kinds of things.

You can do math in the Emacs shell, using prefix notation.

Prefix notation is when you put the operator first, then the operation, in parenthesis (remember, Emacs is written in Lisp).

So, in Emacs shell, to multiply a few numbers, you could type:

(* 3 2 6)

Then press return to see the result - 36.

You can also, in a file itself (like this one, where I'm writing this post), type control-x control-e at the end of a line with a command that's valid in Emacs Lisp, to see the result.

If I'm writing something financial - hmm, what's 3.234 times 5? - I can write it out and spit out the answer in the same line - (* 3.234 5)=16.17.

Another powerful feature of Emacs. You'll come to appreciate these, as time goes on.

Org Mode

There are a variety of special modes you can use in Emacs, that are good for gettting specialized work done.

I'll just focus on one of them: org mode.

I actually use the most simple, basic version of org mode: an asterisk to make a line a section heading, and another asterisk to start a sub-heading under that heading.

Keep adding asterisks to keep adding sub-sub-layers (as many as you want).

Think of them as 'levels'. An asterisk creates a level. Another one creates a sub-level. Sub-levels can have sub-sub-levels, unto infinity.

Press tab to expand a level, and tag again to contract it.

Just use the file extension .org and Emacs will know what to do with your file, and import this functionality.

You can get much more advanced - way more advanced - than the simple model I've outlined here, but that's what I use, and it's been extraordinarily helpful.

Parting Thoughts

That concludes the whirlwind tour.

For Emacs Lisp, if you want to go down that customization road, search for code samples online.

You'll find a lot, though in my experience, a lot of functionality already exists in pre-written functions, and much of the work is really just learning how to string those together.

It took me about a year to learn Emacs. From the first year on, I could do pretty much anything I want, that's possible in a text editor.

There are some times, incidentally, when maybe it's not easiest to manipulate a thing in Emacs. It might be easier to manipulate a file using a scripting language, and just use the Emacs shell to run the command. That's fine. But if you want Emacs to do something for you, Emacs, almost always, has a way.

And by the time you've been using Emacs as long as I have (4 years now), you'll never want to switch editors again.