21

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! – pancake Jul 4 '12 at 19:04
  • 2
    Perhaps you are thinking of the closeb script. – Aditya Jul 4 '12 at 22:17
  • 1
    Tested the closeb script, works very well. – Rmano Feb 4 '14 at 0:22
14

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

  • 1
    Thank you, this works. Is there any shortkey that also autocomplete the nameofenviron to nameofenvironment? – alhelal 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. – jdhao Mar 23 at 8:46
2

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 – Dai Bowen 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. – Andrew Aug 16 '16 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).

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.