I was about to write a blog post about how indifferent I became towards different text editors, and how I don’t really care anymore whether I edit code with emacs, vim, Sublime or even Atom. And then this happened:
It’s called Spacemacs and it’s a beautiful hybrid between emacs and vim, or at least it looks like it. Will explore how it works in the course of the next couple of days, i.e., no work will be done and I will spend my days playing with the configuration of a text editor.
And I thought those days were over. Silly me.
update, Sep 9: I’m back on ST3 vim with my old config. I still think Spacemacs is a great idea, it’s just a bit too bloated for my taste.
The biggest problem with Emacs is that it’s not a particularly intuitive piece of software, to say the least, hence many users flee after their first encounter with it. Emacs has its complicated keyboard shortcuts, enormous documentation and config files written in a Lisp dialect (called Emacs lisp), so at first it might seem very unpleasant using it. However, once tamed, it becomes your best friend.
I’d like to present some tips that customize Emacs making it a perfect and very sophisticated editor for LaTeX. Most of these ideas are taken from various config files, howtos and other resources found on the web. Some of them are mine, but I can no longer tell which.
First things first: you need to get Emacs and AUCTeX, and get it running.
Every major linux distro comes with both Emacs and AUCTeX available via package systems. In Ubuntu, you just type sudo apt-get install emacs23 auctex and you’re laughing. Emacs is also available for Windows, and AUCTeX website has instructions on how to set it up with Windows systems. Mac users have a choice of setting up either Carbon Emacs (a version closer to original GNU Emacs) or Aquamacs (an Emacs variant supporting tabs and other nice tweaks; preferred Emacs package by all Mac users I know, Karolina included). Full comparison of Mac Emacs variants is available here, so you can make your own choice. Both Carbon Emacs and Aquamacs come with AUCTeX bundled, so there’s no need to download additional packages.
After running Emacs and loading a TeX file (C-x C-f file_name.tex), AUCTeX should load itself automatically. If it doesn’t happen, you can invoke it with M-x tex-mode, or you can put the following into your $HOME/.emacs file:
(all the code snippets from this post are available as a Github Gist)
The options above will make sure, that AUCTeX macros are loaded every time a TeX file is opened. The last option, ;(setq TeX-PDF-mode t), is commented (all lines beginning with ; are a comment in Emacs Lisp), but you can uncomment it if you want to have PDFLaTeX mode enabled by default for all documents.
AUCTeX has a number of nice features, the two I use most often are:
automatic formatting of a section: C-c C-q C-s;
section preview: C-c C-p C-s; (see the screenshot on the right)
Preview function is very nice, because you can see the commands that are behind preview images, edit the code, apply preview again and see the results — no need to parse the whole file too often, and most importantly no need to switch to a PDF/PS viewer to see if your math formula/xypic tree is formatted correctly. Trust me, this saves a lot of time.
(update 27.12.2012: there’s another way of previewing LaTeX symbols inside an Emacs buffer, take a look here)
AUCTeX has many many more features, and you can always consult its documentation if you want to learn more. It’s a little bit overwhelming, but learning it is a very good investment, especially if you work with TeX a lot. But there are more packages that provide features which make your life easier.
Flymake is one of those packages. It enables Emacs to check the syntax of your TeX file on-the-fly. To turn it on, put the following code in your $HOME/.emacs:
Beware, though — flymake consumes quite a lot of CPU power, especially when used with large files (and paradoxically large files make it most useful).
On the other hand, spell-checking while you type isn’t so cpu consuming, and you can turn it on with:
(setq ispell-program-name "aspell") ; could be ispell as well, depending on your preferences
(setq ispell-dictionary "english") ; this can obviously be set to any language your spell-checking program supports
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-buffer)
Another nice package is the Outline Mode. It allows the user to hide some parts of the text file, which makes working with large files much easier. To enable it, put the following in $HOME/.emacs:
Now you can fold sections, subsections, chapters, or the whole document. To hide all the contents of your current section, use C-c C-o C-l. You can apply it to a chapter, subsection, etc. You can also move to a next unit of your document with C-c C-o C-n, or to a previous one with C-c C-o C-p. If you’re lost and want to see the whole document again, use C-c C-o C-a.
Folding and unfolding parts of the text might be confusing, though, but there’s another way to navigate through a big TeX file, and you can use Reftex mode for it. Reftex is a mode that helps with managing references (full documentation), but it can also be used to create a table of contents for a TeX file and to navigate using it. Here is my configuration for Reftex from my .emacs file:
Once Reftex is loaded, you can invoke the table of contents buffer with C-c =
All right, enough. If I mention any more packages, I guess it will scare off those who aren’t already scared. I know that Emacs is a bit peculiar with its complicated keyboard shortcuts, enormous documentation and thousands of modes. It’s not easy to learn, but definitely worth it. I remember that switching from Vim to Emacs for LaTeX editing wasn’t easy, but I never regretted that, and I hope whoever’s going to switch under the influence of this post will not regret it either.