# Get emacs to automatically insert \end{} commands after a \begin{} command

I've recently switched from Kile to Emacs/AUCTeX for LaTeX editing. I like AUCTeX a lot more in general, in part because I've put a lot of effort into learning Emacs really well. One feature I really miss from Kile is that it will automatically insert an \end{environment} whenever I enter a begin{environment}. Since I'm going into and out of equations a lot, this saves me a lot of typing.

Is there a way to get emacs to automatically insert \end{} whenever I type a \begin{}, the way kile does?

-
Ctrl-c Ctrl-e (the - means that you press the second button while holding the first) opens the mini-buffer where you insert the environment and emacs then automatically inserts \begin{} and \end{}. It also remembers the history of the commands so that you don't have to always write it yourself – Pier Paolo Jan 15 '14 at 21:19
C-c ] closes the current open env. As Pier says, learn to use C-c C-e to insert ends, then learn to use C-u C-c C-e to change the current env to something else. Imo the best feature in auctex – daleif Jan 15 '14 at 21:33
@Pier Paolo: Do you want to expand that into an answer? – Dan Jan 15 '14 at 21:46
@Dan: sorry, I'm a beginner so I only know a few basic tricks that I found surfing the Net, but you can easily find beginners guides with your search engine – Pier Paolo Jan 15 '14 at 21:56
@Pier Paolo: That earlier comment was a hint that your first comment was already a good answer, and that I would like to upvote and possibly accept it. – Dan Jan 15 '14 at 22:10

AUCTeX has autocompletion mechanisms different from most of the other LaTeX editors. In Emacs, when TeX-latex-mode is activated, the sequence Ctrl-c Ctrl-e (the - means that the second key has to be pressed while holding the first, while the space implies the release of both keys before the next combination) opens the mini-buffer dialog interface at the bottom of the page where one types the name of the environment desired. Press enter and Emacs then automatically inserts both \begin{} and \end{} tags and places the pointer in the line between said tags, usually even adding proper indentation. AUCTeX keeps the history of environments inserted in this way, so you can recall a recently inserted environment using the up and down arrow keys. In addition, the last inserted environment will be used by default, so if you insert an environment very often you don't need to type anything at all.

Moreover, as mentioned by @daleif, you can change the current environment with Ctrl-u Ctrl-c Ctrl-e and insert an \end{...} matching the last open \begin{...} with Ctrl-c ].

More in depth information can be found at the official project page.

Please note that in the documentation a non-conventional symbolism is used: e.g., C stands for Ctrl, RET for the enter (return) button, M for the diamond shaped Meta key in old keyboards (but alt does the job allright), etc.

-

If you are editing a file with .tex extension, then emacs should automatically enter latex-mode (you will see (LaTeX) in the status bar).

In this mode, C-c C-o calls latex-insert-block which will prompt for the block name and arguments, with history and automatic name suggestion. This will insert both the beginning and end commands.

If you already typed the beginning command, C-c C-e ends the block calling latex-close-block.

You can check all features of latex-mode by pressing F1 m while editing a LaTeX file.

Other features include directly compiling from the buffer with C-c C-f, or checking for inconsistencies.

-
Just to be clear: you are talking about the Emacs builtin latex-mode, not AUCTeX. – giordano Jan 15 '14 at 21:37
There is not much reason to edit latex in emacs without auctex ;-) – daleif Jan 15 '14 at 22:34
Is there a way to get this convenience in latex blocks in org-mode? – Trevor Alexander Jan 16 '14 at 1:22
@TrevorAlexander -- You are more likely to get an answer (though not by me) if you post that as a separate question. – jon Jan 16 '14 at 3:39