Vim Tips Wiki
(Created page with '{{TipNew |created=February 21, 2010 |complexity=basic |author= |version=7.2 |category1=LaTeX }} If you don't like [http://vim-latex.sourceforge.net/ latex-suite]'s method for ins…')
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{TipNew
 
{{TipNew
  +
|id=1647
  +
|previous=1646
  +
|next=1648
 
|created=February 21, 2010
 
|created=February 21, 2010
 
|complexity=basic
 
|complexity=basic
 
|author=
 
|author=
|version=7.2
+
|version=7.0
  +
|subpage=/201002
|category1=LaTeX
 
  +
|category1=Automated Text Insertion
  +
|category2=Completion
 
|category3=LaTeX
 
}}
 
}}
If you don't like [http://vim-latex.sourceforge.net/ latex-suite]'s method for inserting environments, here's a simple solution that's closer to what [http://www.gnu.org/software/auctex/documentation.html AUCTeX] does.
+
If you don't like [http://vim-latex.sourceforge.net/ latex-suite]'s method for inserting environments, here is a simple solution that is closer to what [http://www.gnu.org/software/auctex/documentation.html AUCTeX] does.
Running <tt>:E environment</tt> puts <tt>\begin{environment}</tt> and <tt>\end{environment}</tt> above and below the current line (or selection). Running <tt>:Ei environment</tt> does the same thing but leaves you in insert mode. The environment names returned by <tt>ListE</tt> will tab complete.
 
   
 
Running <code>:E environment</code> puts <code>\begin{environment}</code> and <code>\end{environment}</code> above and below the current line (or selection). Running <code>:Ei environment</code> does the same thing but leaves you in insert mode. The environment names returned by <code>ListE</code> will tab complete.
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
 
 
fun ListE(A,L,P)
 
return "align*\nenumerate\nitemize\nfigure\ntabular\nbmatrix\npmatrix\ncases\n\ndocument\narray\nproof"
 
endfun
 
   
  +
The tab completion of environments is accomplished through the <code>-complete=custom,ListE</code> 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.
You can then use a mapping to quickly insert environments while in insert mode
 
  +
<pre>
imap <buffer> <C-Space> <Esc>:Ei<Space>
 
 
command -complete=custom,ListE -nargs=1 -range E normal <line1>ggO\begin{<args>}<Esc><line2>ggjo\end{<args>}<Esc><line1>ggv<line2>ggjj=
or even create mappings to insert specific environments that you use very frequently, for example
 
 
command -complete=custom,ListE -nargs=1 Ei execute "normal \<Esc>i\\begin{<args>}\<CR>\\end{<args>}<Esc>O<Space>" | startinsert
imap <buffer> <C-e> <Esc>:Ei align*<CR>
 
 
function ListE(A,L,P)
 
return "align*\nenumerate\nitemize\nfigure\ntabular\nbmatrix\npmatrix\ncases\n\ndocument\narray\nproof"
  +
endfunction
  +
</pre>
  +
 
You can then use a mapping to quickly insert environments while in insert mode:
  +
<pre>
 
imap <buffer> <C-Space> <Esc>:Ei<Space>
  +
</pre>
  +
 
Or even create mappings to insert specific environments that you use very frequently, for example:
  +
<pre>
 
imap <buffer> <C-e> <Esc>:Ei align*<CR>
  +
</pre>
  +
  +
==Comments==

Latest revision as of 06:43, 13 July 2012

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"
endfunction

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>

Comments[]