# Shortcut for inserting matching \end{something} in VIM

I remember some time ago I always used a shortcut in VIM that allowed me to type this (<X> is indication of the location of the cursor):

... rest of document ...

\begin{<ENVIRONMENT>}
some text
more text
<X>

... document continues ....


and then I would key in some shortcut at the location of <X> and the matching \end{<ENVIRONMENT>} would be inserted:

... rest of document ...

\begin{<ENVIRONMENT>}
some text
more text
\end{<ENVIRONMENT>}

... document continues ....


Here, of course, <ENVIRONMENT> can be anything, like align or itemize.

I know about VIM's <C-x> + <C-p>, <C-x + <C-n> and <C-x> + <C-o>. The last one works very well for closing HTML tags, but it doesn't work in LaTeX.

I forgot what it was and I hope someone here knows it. It might have been a plugin but I don't think so. If anyone can tell me what it is, I will be forever grateful.

EDIT: Of course I am using LaTeX-Suite (or vim-latex, wish they'd stick to a name).

• I'm not referring to that, but nice to know, it's basically as useful as what I'm looking for, thank you! Jul 4 '12 at 19:04
• Perhaps you are thinking of the closeb script. Jul 4 '12 at 22:17
• Tested the closeb script, works very well. Feb 4 '14 at 0:22

## 6 Answers

When using VimTeX, you can type ]] in insert mode to close the current environment.

Something that is pretty much as useful is to type nameofenvironment then F5 which gives

\begin{nameofenvironment}

\end{nameofenvironment}<++>


and puts the cursor in the body of the environment. You can leave the environment easily by pressing CTRL J

• Thank you, this works. Is there any shortkey that also autocomplete the nameofenviron to nameofenvironment? Oct 27 '17 at 4:19
• Does not work for Neovim without configuration. At least it should be pointed out what should be configured for the shortcut F5 to work. Mar 23 '19 at 8:46
• This does not work for me.
– Soap
May 9 '20 at 14:17

All of the solutions above close an environment for you after you have typed \begin{<environment name>}... but I prefer to just type the name of the environment and then have vim fill in the \begin{...} and \end{...} tags for me. To do this I have the following lines in ~/.vim/ftplugin/tex.vim:

" put \begin{} \end{} tags tags around the current word
map  <C-B>      YpkI\begin{<ESC>A}<ESC>jI\end{<ESC>A}<esc>kA
map! <C-B> <ESC>YpkI\begin{<ESC>A}<ESC>jI\end{<ESC>A}<esc>kA


This allows me to type a word at the start of a line, such as align, itemize, theorem, ... then control-B changes it into the mark-up for the environment. For example, if I type align* followed by control-B then vim translates this into:

\begin{align*}
\end{align*}


A useful feature of my macro is that it preserves indentation of the environment name.

EDIT As pointed out is the comments, this is a standard part of vim-latex.

• cmhughes' answer describes the same function as this, which is built into vim-latex Aug 16 '16 at 11:30
• @DaiBowen Thanks. I missed this. I won't delete the post as it provides the details of one way to do this without using vim-latex, which I at least don't use.
– user30471
Aug 16 '16 at 11:54

You search for the closeb plugin. It does exactly what you describe.

If you enable it and press Ctrl+_ in insert mode, it closes the current environment.

put in your vimrc:

inoremap \end <><Esc>?begin<Enter>Vy/<><Enter>pfbcwend<Esc>kddo


How it works: innoremap create a mapping keys in insert mode when you insert \end. put a "<>" string, find(/) the word "begin" and copy the entire line (Vy), then back to the "<>" (\<>) and paste the line (p), find the first b in the line and change the word (cw) for "end" after that delete the "<>" string and put in insert mode.

So: The text must be like that:

\begin{something}
some texot o comands


In diferents lines. It must work.

If you use the following incantation

map! <C-A> <ESC>"ayy"aPi\begin{<ESC>Ea}<ESC><CR>i\end{<ESC>Ea}<ESC>O


then, using A while in insert mode, the line

abc


turns into

\begin{abc}

\end{abc}


with the cursor in between (and still in insert mode).

• The characters got interpreted and not printed (:-): Jun 29 '19 at 20:17
• :map! <C-A> <ESC>"ayy"aPi\begin{<ESC>Ea}<ESC><CR>i\end{<ESC>Ea}<ESC>O Jun 29 '19 at 20:22