127

One 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 simply 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 than 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.

Well... Now it's your turn...

2
  • 8
    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
    Commented Jun 15, 2011 at 17:17
  • 1
    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
    Commented Jun 17, 2011 at 15:31

7 Answers 7

123

Summary answer

This answer is CW. Please add the best tricks to it!

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 (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 and description, 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 for empheq environment provided by the package by the same name. Before:

    \begin{empheq}{alignat=2}
      &&& *!*
    \end{empheq}
    

    After:

    \begin{empheq}{alignat=2}
      &&& \\
    *!*&&&
    \end{empheq}
    
8
  • I may have missed stuff in my summarising, please add stuff I've missed.
    – Seamus
    Commented Jun 17, 2011 at 16:26
  • 3
    Not strictly about key-bindings, but Orgmode Tables is really very cool. Pointed out to me by Damien & Oscar. Commented Jun 19, 2011 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
    Commented Jun 20, 2011 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). Commented Jun 20, 2011 at 15:21
  • 1
    Oh. That reftex trick of changing the level of headers with < and > is AMAZING. emacs never ceases to amaze me…
    – Seamus
    Commented Jan 9, 2012 at 16:15
16

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
2
  • 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). Commented Jun 16, 2011 at 23:20
  • 2
    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
    Commented Jun 17, 2011 at 15:05
12

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.

7
  • 1
    C-c . and C-c *, I think, not C-c C-. and C-c C-*. Commented Jun 16, 2011 at 12:10
  • @BeSlayed You're right there. Ooops.
    – Seamus
    Commented Jun 16, 2011 at 12:17
  • Instead of C-C C-m you can do C-C Ret.
    – N.N.
    Commented Oct 20, 2011 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
    Commented Nov 21, 2014 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
    Commented Nov 23, 2014 at 13:50
9

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).

4
  • 1
    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
    Commented Jun 16, 2011 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
    Commented Jun 16, 2011 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
    Commented Jun 17, 2011 at 15:24
  • @Seamus: right, good idea! I'll pull that into my answer as well.
    – Ben Liblit
    Commented Jun 17, 2011 at 15:25
4

Please google 'refcard auctex'. If you installed Auctex, the pdf is on your computer already. The refcard lists many usefull shortcuts.

4

I think this deserves a place here, even though it is connected rather to Emacs than AucTeX itself (it really can save you a lot of time!).

There is a great package called yas-snippet which can facilitate many things one does while editing TeX files in Emacs.

For example I like using easy-todo for notes. But writing \todo{note} everytime I want to make a note is a bit of a turnoff. With yas-snippet I can press just td <TAB> (or any other key combination I set) and auto-magically \todo{note} appears with the cursor set in the brackets.

  • To install in Emacs 24 just do: M-x list-packages and choose yas-snippet.
  • To make a new snippet: M-x yas-new-snippet
  • To compile a snippet: C-c C-c

Sample code for notes (mentioned above):

# -*- mode: snippet -*-
# name: easy todo note
# key: td
# binding: direct-keybinding
# type: snippet
# --
\todo{ ${1:note}}

Some more things to note:

  • you can use this pretty much for everything. E.g. if it seems that there is no keyboard shortcut (for a macro) you are looking for, you can always define a new snippet and bind it to the key of your choice.

  • it facilitates specifying packages one wants to load. Using a macro for \usepackage{} can take awfully long time (as someone above mentioned). But if you know the name of the package, and have yas-snippet installed you can just type pkg <TAB> and enter the name of the package.

  • yas-snippet comes with a bunch of predefined snippets for LaTeX. You can easily learn about them through menu-bar (M-x menu-bar-mode if it is not enabled).

More info here.

1

mostly after a comma

you needn't be consistent in the number of spaces you type.

so I would have something like

(global-set-key (kbd ",")
    (lambda () (interactive)
      (insert ", ")))

with periods is tricky, because usually arguments can be involved, and it breaks the whole thing really.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .