Useful shortcuts or key bindings or predefined commands for emacs+AUCTeX
Summary answer
This answer is CW. Please add the best tricks to it!
First things first: these cheat sheets are very useful:
- AUCTeX refcard
- Emacs refcard
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
(or C-c RET
) 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 (\begin
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-a
runs LaTeX/TeX, BibTeX/Biber, Makeindex, etc... as appropriate until the document is ready, and eventually opens the output document in the viewer. It is like running C-c C-c
(see below) repeatedly, but with just a single key binding. This is what you want to use most of the time if you trust the advanced ability of AUCTeX to guess the right commands to run.
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 want to do. You can use this key binding also to delete all auxiliary files created during compilation (Clean
and Clean all
actions).
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.
M-RET
or C-c C-j
has different effects depending on the current
environment:
In list environments like
itemize
,enumerate
anddescription
, it inserts a new line and an\item
macro. In this example,*!*
indicates the position of cursor (point in Emacs jargon).\begin{itemize} \item Text *!* \end{itemize}
Hitting
M-RET
results in:\begin{itemize} \item Text \item *!* \end{itemize}
In tabular like environments, it includes a
\\
to start a new line and necessary number of&
by parsing the table specification argument. Again,*!*
indicates the position of cursor (point)\begin{tabular}{lll} 1 & 2 & 3 *!* \end{tabular}
Hitting
M-RET
results in:\begin{tabular}{lll} 1 & 2 & 3 \\ *!*&& \end{tabular}
In some math environments, it also includes a
\\
and necessary number of&
. Example forempheq
environment provided by the package by the same name. Before:\begin{empheq}{alignat=2} &&& *!* \end{empheq}
After:
\begin{empheq}{alignat=2} &&& \\ *!*&&& \end{empheq}
In addition to what Seamus mentioned:
-
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. C-c ;
comment out/uncomment regionM-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:
C-c =
show (RefTeX's internal) table of contents - very useful for navigating long documentsC-c [
insert citation with key selection (lets you search through your bibliographic references and then insert) (alsoC-u C-c [
- same thing but prompts for optional arguments)And of course:
C-c C-c
run (the next appropriate) command on master file (LaTeX, BibTeX etc.)and
C-c ‘
- view errors / next errorAnd, not part of the AUCTeX/RefTeX package, but the
- align commands from
align.el
can be useful for dealing with tables in LaTeX
C-c C-s
create section with optional label
C-c C-e
create environment (\begin
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.