# Useful shortcuts or key bindings or predefined commands for emacs+AUCTeX

On of the problems I've encountered with emacs in general, and with the defaults key bindings in particular, is the diversity. There are simply so many of them, that sometimes, at the end of the day, you simple don't know that you should look for something which is already there.

Are you aware of a place where these can be found? I'm not referring to a listing of all possible key bindings etc. (which can probably be found in AUCTeX documentation), rather then sort of the top 10-20-50 or so. If something like this is not around, maybe having a wiki here would be a nice idea.

-
This is a great question. I'm always learning cool new stuff about emacs. (For example, I only learned about C-c C-m last week!) –  Seamus Jun 15 '11 at 17:17
I might write up a CW answer containing all the information below (sorted by topic). It seems that each answer contains good information and accepting one or the other seems odd somehow. –  Seamus Jun 17 '11 at 15:31

First things first: these cheat sheets are very useful:

C-h m will show information on the current major mode.

C-c C-h shows all bindings that start with C-c

C-h b shows all bindings currently available.

Now for some details.

## Macros

C-c C-m insert macro (warning: pressing TAB at this point will take a while because auctex will load a list of all the macros it knows about. If the macro you pick is \usepackage and then TAB auctex will load a list of all the packages it knows about for you to choose from.)

C-c C-f C-e Add an \emph{} and place cursor between the braces. Or if you have some text marked, wrap it in \emph replace C-e by C-b, C-c C-i, C-t for \textbf \textsc \textit or \texttt respectively. C-c C-f ? will give you a list of all the possibilities. This command knows about mathmode and will behave accordingly inside dollars or in equation environment. Also, with a prefix argument C-u, it will change the innermost surrounding font command accordingly, so if you have point at * in \textit{This is* bold} and press C-u C-c C-f C-b, you get \textbf{This is* bold}. Even though it's four keystrokes, it can be very useful.

## Sections/Environments

C-c C-s create section with optional label

C-c C-e create environment (\start and \end tags) choose from an autocomplete list or type your own. (If you have LaTeX mode on in an empty file, this will default to the document environment, and prompt you for a documentclass.

C-u C-c C-e change the type of the current (innermost) environment

C-c . mark current environment

C-c * mark current section/subsection

C-c ] close current environment

C-M-a find matching begin environment, C-M-e find matching end

## Compilation

C-c C-c do most appropriate compilation activity (LaTeX, BibTeX, View...) It's pretty smart, and you can override the default to pick what action you cant to do.

C-c C-r do most appropriate compilation activity (LaTeX, BibTeX, View...) to the region that has been pinned by C-c C-t C-r. Of course one wants to use C-c . or C-c * to ease the select a region.

C-c C-v view document (without compiling)

## RefTeX

RefTeX, while not strictly part of AucTeX is an essential part of your emacs/TeX working life.

C-c ( add label. Auto-suggests names based on current section.

C-c ) add \ref you can select from the list of currently defined labels. If you customise a certain variable (whose name escapes me) you can add hyperref, varioref and cleverref reference commands to the list of types of reference available.

C-c [ add citation. RefTeX understands bibliography commands and will search your .bib for references that match the pattern you supply to this command. Can be customised to have harvard, chicago and other kinds of reference commands available. (No BibLaTeX citation style support yet as far as I know.)

C-c = jump to section: opens list of sections/subsections etc you can jump to.

When in the toc buffer :

RET Go to the selected header and hide the toc buffer. If the local variable TeX-Master is correctly set (see AUCTeX documentation), it is a very convenient way of jumping in any other file of your project.

Space Go to the selected header, don't hide the toc buffer.

< Increase the level of the selected header or region (section becomes chapter, subsection becomes section, etc)

> Reduce the level of the selected header or section.

M-% Search-and-replace regexp in the whole document

RefTeX also provides a lot of convenient options through its menu, including conflicting label detection and fix (runs a search-and-replace query), document-scaled grep, document-scaled search and replace.

## Miscellaneous

LaTeX-math-abbrev-prefix is a useful thing which is  by default.

This gives you access to a bunch of mathmode symbols in two keystrokes. For example, you can get \subset by typing  {

By customising LaTeX-math-list you can get whatever you like out of this prefix command. A bunch of the most useful are on the refcard.

C-c ; comment/uncomment region

C-c C-o C-b for folding TeX code

C-M-S f or C-M-S b to mark the content between a pair of balanced braces.

-
I may have missed stuff in my summarising, please add stuff I've missed. –  Seamus Jun 17 '11 at 16:26
Not strictly about key-bindings, but Orgmode Tables is really very cool. Pointed out to me by Damien & Oscar. –  emacsomancer Jun 19 '11 at 22:25
@BeSlayed I think that is a bit beyond the remit of this question, but it's certainly a neat little tool. –  Seamus Jun 20 '11 at 10:49
Yes, and maybe this should be part of a separate question/topic, but it's another feature which makes using Emacs for LaTeX extremely useful (and I would love to learn more like this). –  emacsomancer Jun 20 '11 at 15:21
Oh. That reftex trick of changing the level of headers with < and > is AMAZING. emacs never ceases to amaze me… –  Seamus Jan 9 '12 at 16:15

C-c C-s create section with optional label

C-c C-e create environment (\start and \end tags) choose from an autocomplete list or type your own. (If you have LaTeX mode on in an empty file, this will default to the document environment, and prompt you for a documentclass.

C-u C-c C-e change the type of the current (innermost) environment

C-c . mark current environment

C-c * mark current section/subsection

C-c C-m insert macro (warning: pressing TAB at this point will take a while because auctex will load a list of all the macros it knows about. If the macro you pick is \usepackage and then TAB auctex will load a list of all the packages it knows about for you to choose from.)

These are the bindings I use the most that are AucTeX specific. There's a useful cheat sheet here. You might also want the generic emacs one here.

I don't know if this is on by default, but the funky quote thing is another lifesaver. See this answer for some details. The basic idea is that  followed by a keystroke can give you a bunch of different shortcuts to macros. It's like a compose key for LaTeX mathmode.

-
C-c . and C-c *, I think, not C-c C-. and C-c C-*. –  emacsomancer Jun 16 '11 at 12:10
@BeSlayed You're right there. Ooops. –  Seamus Jun 16 '11 at 12:17
Instead of C-C C-m you can do C-C Ret. –  N.N. Oct 20 '11 at 22:15
@Seamus when I hit C-c C-e then TAB I get auto-completion list but I didn't find \includegraphics even when graphicx package in preamble, I wonder how to have commands of laoded packages in the preamble appear in the auto-completion list. –  doctorate Nov 21 '14 at 19:10
@doctorate \includegraphics isn't an environment. So it's not surprising it doesn't show up. C-c C-e figure gets you a figure environment. I guess you could use C-c C-m to get you includegraphics but it hardly seems worth it, in terms of the time it saves you. –  Seamus Nov 23 '14 at 13:50

In addition to what Seamus mentioned:

1. I use the font selection commands a lot, e.g. C-c C-f C-b for insert \textbf{} (bold) or wrap \textbf{} round current selection, if there is one. C-c C-f C-c - same thing for small caps etc.
2. C-c ; comment out/uncomment region
3. M-q format paragraph (sticks in and removes line breaks so that there is no wrapping or over-short lines)

The following are actually RefTeX command properly speaking, but useful:

4. C-c = show (RefTeX's internal) table of contents - very useful for navigating long documents

5. C-c [ insert citation with key selection (lets you search through your bibliographic references and then insert) (also C-u C-c [ - same thing but prompts for optional arguments)

And of course:

6. C-c C-c run (the next appropriate) command on master file (LaTeX, BibTeX etc.)

and

7. C-c ‘ - view errors / next error

And, not part of the AUCTeX/RefTeX package, but the

8. align commands from align.el can be useful for dealing with tables in LaTeX
-
For commenting, I use the general M-; (not mode specific). next-error is bound to C-x  (and also to M-g n -- "goto next" -- in newer Emacsen; these are also more general than just auctex-specific). –  imz -- Ivan Zakharyaschev Jun 16 '11 at 23:20
I just discovered C-c C-f and was about to add them to my answer when I saw this! Always learning... Good points about the reftex commands: might also be worth mentioning C-c ( for add label and C-c ) for add reference. –  Seamus Jun 17 '11 at 15:05

C-h m is useful for identifying bindings provided by the current major editing mode. (Special thanks to @Seamus for pointing this out in a comment below.) Per the Other Help Commands section of the GNU Emacs Manual:

Each Emacs major mode typically redefines a few keys and makes other changes in how editing works. C-h m (describe-mode) displays documentation on the current major mode, which normally describes the commands and features that are changed in this mode.

If you have reason to believe that most interesting AUCTeX bindings begin with C-c, then C-c C-h will show you just the key bindings that begin with C-c. There is an informal convention that major modes use C-c as a prefix, so this is a reasonable thing to try.

If you wanted all possible key bindings (which the original question does not want), then you could use C-h b. Per the Other Help Commands section of the GNU Emacs Manual:

C-h b displays a list of all the key bindings now in effect: first the local bindings of the current minor modes, then the local bindings defined by the current major mode, and finally the global bindings (see Key Bindings).

-
C-h m will give you all the major mode bindings. So doing that while the active buffer is a tex file should give you the auctex bindings. –  Seamus Jun 16 '11 at 10:00
I explicitly wrote that I'm not looking for ALL bindings. I am looking for those that people here use often. Thnx for the tip anyway! –  Dror Jun 16 '11 at 20:01
@Dror: oops, you are right. I read your question inattentively and missed the important “not”! I'll revise my answer slightly to avoid claiming that you asked for something you didn't. –  Ben Liblit Jun 17 '11 at 15:24
@Seamus: right, good idea! I'll pull that into my answer as well. –  Ben Liblit Jun 17 '11 at 15:25