If you are in the preamble of a TeX document you can do C-s \begin{document}
or C-r \begin{document}
if you are in the body of the document. It would useful to have a simple, all-purpose macro (say, M-D
) that would accomplish this no matter where you are in the document.
1 Answers
It’s easy. First start by recording a macro. You do this by calling kmacro-start-macro-or-insert-counter
, which by default is bound to F3
. Then perform the actions you want your macro to do. In this case, I recommend running C-<home>
followed by C-s \begin{document}
. Then end your recording by calling kmacro-end-or-call-macro
, which is bound to F4
.
Now you want to give that macro a name, so type C-x C-k n
(kmacro-name-last-macro
). You can also bind it to a key with C-x C-k b
(kmacro-bind-to-key
) if you want to go ahead and start using it (you can also call it by hitting F4
again).
Finally, open your init file and type M-x insert-kbd-macro
. Enter the same name that you gave the macro. This will insert code to define the macro at startup. All you have to add is a key binding, which you can do in the usual way.
Don’t forget to save your init file.

- 17,977
- 1
- 22
- 28
C-s
? Which would be fine if the current location in the document is before\begin{document
, but will fail if current location is after\begin{document
. I imagine that what I am looking for is something that would require some (e)lisp magic, which, unfortunately is beyond me. – sgmoye Aug 17 '22 at 14:35M-< C-s \begin{document}
– Drew Aug 17 '22 at 15:34