# Command to create \begin{} \end{} environment in LaTeX

How can I make a command that allows me to insert LaTeX environments in the current text?

The idea is to press Ctrl+E or type :texenv and then enter the environment name in the bottom line, e.g. :texenv itemize. Which would then insert a

\begin{itemize}
\end{itemize}


Ideally placing the curser after the begin's closing brace.

I suspect I could do this with command but how do I paste the text into the buffer?

• If you're using Vim-LaTeX/LaTeX-Squite, press F5. – wchargin Feb 25 '15 at 16:32

You can create a simple function to insert text with the append() function:

fun! TexEnv(name)
let l:l = line(".") - 1
call append(l:l, "\\end{" . a:name . "}")
call append(l:l, "\\begin{" . a:name . "}")
endfun


You can then use command to call this; with -nargs=1 we tell it to require one argument, and we pass this to our function with <args>:

command! -nargs=1 Texenv call TexEnv("<args>")


Custom commands have to start with an upper case, so I've named it Texenv.

We can use the same function with a key mapping; the input() function asks you for your TeX environment name, and passes that on to the TexEnv() function:

nnoremap <C-e> :call TexEnv(input("TeX environment name: "))<CR>


Note that <C-e> is already bound to scrolling the screen (which is rather useful IMHO), so you're overwriting that mapping...

• Brilliant. fun! TexEnv(name) call append(line("."), "\\end{" . a:name . "}") call append(line(".")-1, "\\begin{" . a:name . "}") endfun seems to do exactly what I was looking for! And I will probably keep it at :Texenv. – bitmask Feb 24 '15 at 17:02

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

### Example

For snipMate, your snippet would look like this; it would be triggered by typing texenv<Tab>:

snippet texenv
\begin{${1:environment-name}} \end{$1}\$2

• I installed snipMate but although it works in c files, the default snips (that already contain a begin snippet) don't load in .tex files. – bitmask Feb 24 '15 at 16:56
• Verify that the 'filetype' corresponds to the snippet file: :setl filetype? – Ingo Karkat Feb 24 '15 at 17:10

If you are using surround.vim. For example add the following to ~/.vim/after/ftplugin/latex.vim

let b:surround_{char2nr('e')} = "\\begin{\1environment: \1}\r\\end{\1\1}"


Now you can do <c-s>b in insert mode to add the surrounding. You can also surround text with other surround operators like ys, yS and v_S.

For more help see:

:h surround
:h surround-customizing