28

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

3
  • I'm not referring to that, but nice to know, it's basically as useful as what I'm looking for, thank you!
    – pancake
    Commented Jul 4, 2012 at 19:04
  • 2
    Perhaps you are thinking of the closeb script.
    – Aditya
    Commented Jul 4, 2012 at 22:17
  • 1
    Tested the closeb script, works very well.
    – Rmano
    Commented Feb 4, 2014 at 0:22

6 Answers 6

28

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

12

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

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

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.

2
  • cmhughes' answer describes the same function as this, which is built into vim-latex
    – Dai Bowen
    Commented Aug 16, 2016 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
    Commented Aug 16, 2016 at 11:54
1

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.

0

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.

-1

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

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

You must log in to answer this question.

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