Vim Tips Wiki

Auctex-style environment creation in LaTeX

1,612pages on
this wiki
Revision as of 00:36, March 27, 2011 by JohnBot (Talk | contribs)

Tip 1647 Printable Monobook Previous Next

created February 21, 2010 · complexity basic · version 7.0

If you don't like latex-suite's method for inserting environments, here is a simple solution that is closer to what AUCTeX does.

Running :E environment puts \begin{environment} and \end{environment} above and below the current line (or selection). Running :Ei environment does the same thing but leaves you in insert mode. The environment names returned by ListE will tab complete.

The tab completion of environments is accomplished through the -complete=custom,ListE part of the command definition. This tells Vim that the tab-completion of this command is defined by the custom function named ListE. This particular completion function is very simple; it always returns the same static text, containing all the possible environments for insertion. It could, however, use the arguments provided to the function in order to customize the completion items depending on context.

command -complete=custom,ListE -nargs=1 -range E normal <line1>ggO\begin{<args>}<Esc><line2>ggjo\end{<args>}<Esc><line1>ggv<line2>ggjj=
command -complete=custom,ListE -nargs=1 Ei execute "normal \<Esc>i\\begin{<args>}\<CR>\\end{<args>}<Esc>O<Space>" | startinsert
function ListE(A,L,P)
  return "align*\nenumerate\nitemize\nfigure\ntabular\nbmatrix\npmatrix\ncases\n\ndocument\narray\nproof"

You can then use a mapping to quickly insert environments while in insert mode:

imap <buffer> <C-Space> <Esc>:Ei<Space>

Or even create mappings to insert specific environments that you use very frequently, for example:

imap <buffer> <C-e> <Esc>:Ei align*<CR>


Around Wikia's network

Random Wiki