Difference between revisions of "Vim"

From Wiki
Jump to navigation Jump to search
m (Update spelling of MUcomplete.)
(41 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
This page is about editing ConTeXt source in Vim, gVim, MacVim, NeoVim, and other Vim clones.
 
This page is about editing ConTeXt source in Vim, gVim, MacVim, NeoVim, and other Vim clones.
The page describes the scripts available with Vim v8.0.0047 or later.
+
The page describes the scripts available with Vim v8.0.0055 or later. If you are using Vim 7, see
If you are using an older Vim, you may copy the <tt>context.vim</tt> scripts from Vim's distribution
+
''Using the scripts with an older Vim'' below.
(https://github.com/vim/vim) under the <tt>runtime</tt> directory, into corresponding
 
folders in your <tt>.vim</tt> folder (so, for example <tt>ftplugin/context.vim</tt> must be copied into
 
<tt>~/.vim/ftplugin/context.vim</tt>).
 
Everything should work, at least with Vim 7.4.
 
  
 
== Using ConTeXt in Vim ==
 
== Using ConTeXt in Vim ==
Line 47: Line 43:
 
An alternative way to typeset a document is to use <tt>:make</tt>. Set the current working
 
An alternative way to typeset a document is to use <tt>:make</tt>. Set the current working
 
directory to the directory of the current buffer, then execute <tt>:make</tt>:
 
directory to the directory of the current buffer, then execute <tt>:make</tt>:
  lcd /path/to/my/project
+
  :lcd /path/to/my/project
 
  :make
 
  :make
 
If a <tt>Makefile</tt> exists in the working directory, it is used (see below for a sample Makefile).
 
If a <tt>Makefile</tt> exists in the working directory, it is used (see below for a sample Makefile).
Line 53: Line 49:
 
If no Makefile is found, <tt>mtxrun</tt> will be invoked directly.
 
If no Makefile is found, <tt>mtxrun</tt> will be invoked directly.
 
Note that <tt>:make</tt> always performs synchronous typesetting. Also, if there are
 
Note that <tt>:make</tt> always performs synchronous typesetting. Also, if there are
errors, the quickfix list is populated, but you have open it manually with <tt>:copen</tt>.
+
errors, the quickfix list is populated, but you have to open it manually with <tt>:copen</tt>.
  
 
It is recommended that you map the above commands. For example, you may add
 
It is recommended that you map the above commands. For example, you may add
 
   nnoremap <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>
 
   nnoremap <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>
to your <tt>.vimrc</tt>, or (better)
+
to your <tt>vimrc</tt>, or (better)
 
   nnoremap <buffer> <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>
 
   nnoremap <buffer> <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>
 
to <tt>~/.vim/after/ftplugin/context.vim</tt>, after which pressing <tt>\tt</tt> (where <tt>\</tt> is your leader key)
 
to <tt>~/.vim/after/ftplugin/context.vim</tt>, after which pressing <tt>\tt</tt> (where <tt>\</tt> is your leader key)
Line 67: Line 63:
 
  let g:context_mtxrun = 'PATH=$HOME/Applications/ConTeXt-Beta/tex/texmf-osx-64/bin:$PATH mtxrun'
 
  let g:context_mtxrun = 'PATH=$HOME/Applications/ConTeXt-Beta/tex/texmf-osx-64/bin:$PATH mtxrun'
  
You may enable synctex by setting <tt>g:context_synctex</tt> to <tt>1</tt> (the default is <tt>0</tt>):
+
To enable/disable SyncTeX, use <tt>\setupsynctex[state=start]</tt> and <tt>\setupsynctex[state=stop]</tt>, respectively, in your document.
let g:context_synctex = 1
 
  
 
You may pass <tt>mtxrun</tt> additional options by putting them in <tt>g:context_extra_options</tt>.
 
You may pass <tt>mtxrun</tt> additional options by putting them in <tt>g:context_extra_options</tt>.
 
For example:
 
For example:
 
  let g:context_extra_options = '--arrange --autopdf'
 
  let g:context_extra_options = '--arrange --autopdf'
The <tt>--autogenerate</tt>, <tt>--nonstopmode</tt> and <tt>--synctex</tt> options are always included in the
+
The <tt>--autogenerate</tt> and <tt>--nonstopmode</tt> options are always included in the
 
command.
 
command.
  
Line 121: Line 116:
 
Vim searches for files in the locations specified by the <tt>path</tt> option. You may
 
Vim searches for files in the locations specified by the <tt>path</tt> option. You may
 
need to adjust the value of <tt>path</tt> for the above to work (see <tt>:help 'path'</tt>).
 
need to adjust the value of <tt>path</tt> for the above to work (see <tt>:help 'path'</tt>).
 
  
 
=== Integration with MetaPost ===
 
=== Integration with MetaPost ===
Line 132: Line 126:
 
Besides, when you are inside a MetaPost environment, you may press CTRL-X followed by CTRL-O
 
Besides, when you are inside a MetaPost environment, you may press CTRL-X followed by CTRL-O
 
to complete a MetaPost/MetaFun keyword (see below for a list of several autocompletion plugins to streamline this).
 
to complete a MetaPost/MetaFun keyword (see below for a list of several autocompletion plugins to streamline this).
This works out of the box: no configuration is required.
+
This works out of the box: no configuration is required. Watch the asciicast above for a demo.
  
 
=== Integration with other languages ===
 
=== Integration with other languages ===
Line 145: Line 139:
 
The key is the name of the filetype and the corresponding value is name of the command.
 
The key is the name of the filetype and the corresponding value is name of the command.
  
=== TODO ===
+
=== Using the scripts with an older Vim ===
  
* Extract data from texweb and create syntax highlighting definitions for ConTeXt.
+
If you are using an older Vim, you may copy the following scripts from Vim's distribution
* Some essential math support.
+
(https://github.com/vim/vim) into corresponding
* Proper URL highlighting ('%' doesn't start a comment, ...) [request by VnPenguin].
+
folders in your <tt>.vim</tt> folder (so, for example <tt>runtime/ftplugin/context.vim</tt> must be copied into
* Perhaps borrow something from https://github.com/lervag/vimtex or http://vim-latex.sourceforge.net/?
+
<tt>~/.vim/ftplugin/context.vim</tt>):
  
== Other useful Vim plugins ==
+
runtime/autoload/context.vim
 +
runtime/autoload/contextcomplete.vim
 +
runtime/compiler/context.vim
 +
runtime/ftplugin/{context,mf,mp}.vim
 +
runtime/indent/{context,mf,mp}.vim
 +
runtime/syntax/{context,mf,mp}.vim
  
=== Autocompletion ===
+
If you get the following error when you open a ConTeXt or MetaPost document:
  
Vim offers a rich completion mechanism (<tt>:help ins-completion</tt>), but there are several plugins
+
E410: Invalid :syntax subcommand: iskeyword
that improve on it, in particular, to provide automatic completion of keywords:
 
  
* µcomplete[https://github.com/lifepillar/vim-mucomplete]
+
edit the syntax files and remove the <tt>syn iskeyword</tt> or <tt>syntax iskeyword</tt> line.
* Completor[https://github.com/maralla/completor.vim]
+
Instead, put a corresponding command in <tt>~/.vim/after/ftplugin/context.vim</tt>:
* NeoComplete[https://github.com/Shougo/neocomplete.vim]
 
* Deoplete (for NeoVim)[https://github.com/Shougo/deoplete.nvim]
 
* YouCompleteMe[https://github.com/Valloric/YouCompleteMe]
 
* AutoComplPop[https://github.com/vim-scripts/AutoComplPop]
 
* SuperTab[https://github.com/ervandew/supertab]
 
  
The asciicast at the top of this page uses µcomplete.
+
setlocal iskeyword=@,48-57,a-z,A-Z,192-255
  
=== Snippets ===
+
and in <tt>~/.vim/after/ftplugin/mp.vim</tt> for MetaPost:
  
Vim allows you to define abbreviations for frequently used pieces of text (see <tt>:help abbreviations</tt>).
+
setlocal iskeyword=@,_
If you need more sophisticated support for snippets of code, UltiSnips[https://github.com/SirVer/ultisnips]
 
is the plugin to use. Here are a few examples of useful UltiSnips snippets for ConTeXt:
 
  
snippet "s(tart)?" "start / stop" br
+
Everything should work, at least with Vim 7.4.
\start${1:something}$2
 
${3:${VISUAL}}
 
\stop$1
 
endsnippet
 
 
snippet enum "Enumerate" b
 
\startitemize[n]
 
\item ${0:${VISUAL}}
 
\stopitemize
 
endsnippet
 
 
snippet item "Itemize" b
 
\startitemize
 
\item
 
${0:${VISUAL}}
 
\stopitemize
 
endsnippet
 
 
snippet it "Individual item" b
 
\item
 
${0:${VISUAL}}
 
endsnippet
 
 
snippet fig "External figure" b
 
\startplacefigure
 
\externalfigure[${1:${VISUAL}}][$2]
 
\stopplacefigure
 
endsnippet
 
 
Save the above text into <tt>~/.vim/UltiSnips/context.snippets</tt>.  
 
Click on the asciicast linked at the top of this document to see UltiSnips snippets in action.
 
  
=== Outline of a document ===
+
=== TODO ===
  
Tagbar[https://github.com/majutsushi/tagbar] is a useful plugin to display an outline or a table of contents of a document.
+
* Extract data from texweb and create syntax highlighting definitions for ConTeXt.
It uses Ctags, which you must install, too. Ctags does not support ConTeXt out of the box, but it is easy to extend.
+
* Some essential math support.
Create a <tt>.ctags</tt> file in your home directory, then copy and paste the following:
+
* Proper URL highlighting ('%' doesn't start a comment, ...) [request by VnPenguin].
 
+
* Perhaps borrow something from https://github.com/lervag/vimtex or http://vim-latex.sourceforge.net/?
<nowiki>
 
--langdef=context
 
--regex-context=/^[[:space:]]*\\startsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\. \1/s,section/
 
--regex-context=/^[[:space:]]*\\startsubsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\.\. \1/s,subsection/
 
--regex-context=/^[[:space:]]*\\startsubsubsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\.\.\. \1/s,subsubsection/
 
--regex-context=/^[[:space:]]*\\startchapter[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\1/c,chapter/
 
--regex-context=/^[[:space:]]*\\startsubject[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/SUBJ \1/c,subject/
 
--regex-context=/^[[:space:]]*\\startpart[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\1/p,part/
 
</nowiki>
 
 
 
Put this in your <tt>vimrc</tt>:
 
    let g:tagbar_type_context = {
 
          \ 'ctagstype': 'context',
 
          \ 'kinds': [
 
          \ 'p:parts',
 
          \ 'c:chapters',
 
          \ 's:sections'
 
          \ ],
 
          \ 'sort': 0
 
          \ }
 
 
 
That's it! See the image at the top of this document for an example.
 
 
 
=== Using LaTeX-Suite ===
 
 
 
[http://vim-latex.sourceforge.net/ latex-suite] currently doesn't support ConTeXt, but if you use it, here's what you have to do to compile ConTeXt documents:
 
 
 
1. After downloading and installing latex-suite, locate the file "texrc" (usually located in <code>~/.vim/ftplugin/latex-suite</code>). Copy this file to <code>~/.vim/ftplugin/tex/texrc</code>
 
 
 
2. Open this copy in your favorite editor (vim comes to mind...)
 
 
 
3. After line 80 in this file, there is a series of "Compiler rules." Just add this line to the section:
 
 
 
TexLet g:Tex_CompileRule_cont = 'texexec --pdf --nonstopmode $*'
 
 
 
This will add compilation for ConTeXT. In order to use it:
 
 
 
4. When you're in vim normal mode, run this command:
 
 
 
TGTarget cont [that's "colon TGTarger cont"]
 
  
5. Edit your TeX-files, save the changes; when you want to compile, switch to normal mode and just type <code>\ll</code> (that's '' 'backslash el el' '')
+
== Filetype detection ==
 
 
Voila, compilation should start. You'll have to specify this compiler target every timeI you open a TeX-file in Vim. If you want to make this the default compiler, you should have this line in your texrc:
 
 
 
TexLet g:Tex_DefaultTargetFormat = 'cont'
 
 
 
== A note on filetype detection ==
 
  
 
TeX (Plain TeX), LaTex and ConTeXt all use the <tt>.tex</tt> extension for files, which makes it difficult to detect the filetype based on the extension. From Vim 7 onwards, Vim does some intelligent checking to see it the file is <tt>plaintex</tt> or <tt>latex</tt> or <tt>context</tt>.
 
TeX (Plain TeX), LaTex and ConTeXt all use the <tt>.tex</tt> extension for files, which makes it difficult to detect the filetype based on the extension. From Vim 7 onwards, Vim does some intelligent checking to see it the file is <tt>plaintex</tt> or <tt>latex</tt> or <tt>context</tt>.
Line 277: Line 191:
 
Currently no other formats are recognized.
 
Currently no other formats are recognized.
  
* If you use ConTeXt most of the time, but occasionally use LaTeX or Plain TeX, you can add the following to your <tt>.vimrc</tt>
+
* If you use ConTeXt most of the time, but occasionally use LaTeX or Plain TeX, you can add the following to your <tt>vimrc</tt>
 
  let g:tex_flavor = "context"
 
  let g:tex_flavor = "context"
 
* If you only use ConTeXt, you can add the following lines to <tt>filetype.vim</tt>:
 
* If you only use ConTeXt, you can add the following lines to <tt>filetype.vim</tt>:
Line 286: Line 200:
 
so the next time you open a <tt>*.tex</tt> file, Vim will always recognize it as a ConTeXt document.
 
so the next time you open a <tt>*.tex</tt> file, Vim will always recognize it as a ConTeXt document.
  
== A note on spell checking ==
+
== Spell checking ==
  
 
Vim 7 or later has a built-in spell checker. To enable it or disable it, use:
 
Vim 7 or later has a built-in spell checker. To enable it or disable it, use:
Line 297: Line 211:
 
Use lowercase letters (<tt>en_us</tt>, not <tt>en_US</tt>). When you set <tt>spelllang</tt>, Vim offers to
 
Use lowercase letters (<tt>en_us</tt>, not <tt>en_US</tt>). When you set <tt>spelllang</tt>, Vim offers to
 
download the language data into your <tt>.vim</tt> folder, if such language is not available.
 
download the language data into your <tt>.vim</tt> folder, if such language is not available.
You can put the above settings in your <tt>.vimrc</tt> if you like.
+
You can put the above settings in your <tt>vimrc</tt> if you like.
  
 
== Powerful key mappings ==
 
== Powerful key mappings ==
Line 304: Line 218:
 
the prefix for user-defined mappings.
 
the prefix for user-defined mappings.
 
By default, the leader is the backslash character, but that may be changed by the user.
 
By default, the leader is the backslash character, but that may be changed by the user.
For example, to use a comma as a leader, put this in your <tt>.vimrc</tt>:
+
For example, to use a comma as a leader, put this in your <tt>vimrc</tt>:
 
  map <leader> ,
 
  map <leader> ,
 +
Rather than overriding the default leader, you may define an alternative key. The <tt><space></tt> is a good
 +
choice, because by default it has the same function as the <tt><right></tt> key, and it is comfortable to type:
 +
map <space> <leader>  " Use <space> as an alternative leader (backslash can still be used)
 +
 +
=== Clean up auxiliary files ===
 +
 +
The following function can be used to clean up temporary files:
 +
fun! ConTeXtClean()
 +
  let l:currdir = expand("%:p:h")
 +
  let l:tmpdirs = ['out'] " Temporary directories
 +
  let l:suffixes = ['aux', 'bbl', 'blg', 'fls', 'log', 'tuc'] " Suffixes of temporary files
 +
  for ff in glob(l:currdir . '/*.{' . join(l:suffixes, ',') . '}', 1, 1)
 +
    call delete(ff)
 +
  endfor
 +
  for dd in l:tmpdirs
 +
    let l:subdir = l:currdir . '/' . dd
 +
    if isdirectory(l:subdir)
 +
      for ff in glob(l:subdir . '/*.{' . join(l:suffixes, ',') . '}', 1, 1)
 +
        call delete(ff)
 +
      endfor
 +
    endif
 +
    call delete(l:subdir) " Delete directory (only if empty)
 +
  endfor
 +
  echomsg "Aux files removed"
 +
endf
 +
Customize <tt>l:tmpdirs</tt> and <tt>l:suffixes</tt> to suit your needs.
 +
In Windows systems, you may have to replace each slash with a backslash, too.
 +
 +
The following mapping allows you to remove auxiliary files by pressing <tt>\tc</tt>:
 +
nnoremap <silent><buffer> <leader>tc :<c-u>call ConTeXtClean()<cr>
 +
 +
=== Snippets ===
 +
 +
Vim allows you to define abbreviations for frequently used pieces of text (see <tt>:help abbreviations</tt>).
 +
Here are a few examples:
 +
<pre>
 +
fun! Eatchar(pat)  " See :help abbreviations
 +
  let c = nr2char(getchar(0))
 +
  return (c =~ a:pat) ? '' : c
 +
endfun
 +
 +
iab <buffer> ch- \startchapter[title={<c-o>ma}]<cr><c-o>mb<cr>\stopchapter<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> s- \startsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> ss- \startsubsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsubsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> sss- \startsubsubsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsubsubsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> slide- \startslide[title={<c-o>ma}]<cr><c-o>mb<cr>\stopslide<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> fig- \startplacefigure<cr><tab>\externalfigure[<c-o>ma]%<cr>[]<c-o>mb<cr><c-d>\stopplacefigure<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
 +
iab <buffer> item- \startitemize<cr><cr>\stopitemize<up><tab>\item
 +
iab <buffer> enum- \startitemize[n]<cr><cr>\stopitemize<up><tab>\item
 +
iab <buffer> i- \item
 +
</pre>
 +
 +
Type the abbreviation followed by Space to expand the snippet, then continue typing normally.
 +
 +
Inside the definition of an abbreviation, marks may be set (see <tt>:h m</tt>), which allow you to jump between the different
 +
parts of a snippet with TAB (CTRL-i) and CTRL-O (in Normal mode) after the abbreviation is expanded (see <tt>:h jump-motions</tt>).
 +
For example, after typing <tt>ch- </tt>, the cursor will be at the title's position. If you press <tt><esc><tab></tt> (or
 +
<tt><c-o><tab></tt> if you want to stay in Insert mode after the jump), you will jump between <tt>\startchapter</tt>
 +
and <tt>\stopchapter</tt>.
  
 
=== Buffer-local Insert-mode macros to speed up editing ===
 
=== Buffer-local Insert-mode macros to speed up editing ===
Line 349: Line 322:
 
You can reset the key mapping to speed ConTeXt compiling.
 
You can reset the key mapping to speed ConTeXt compiling.
  
Just add the following code to your <tt>_vimrc</t>> (or <tt>.vimrc</tt> file under Linux) file:
+
Just add the following code to your <tt>_vimrc</tt> (or <tt>.vimrc</tt> file under Linux) file:
 
<pre>
 
<pre>
 
"run setup and complie, then open the result pdf file
 
"run setup and complie, then open the result pdf file
Line 362: Line 335:
 
"run setup and make list of the current file
 
"run setup and make list of the current file
 
map <F8> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texexec.pl --autopdf --pdf --list --result=%:p:r_list %"<CR><CR>
 
map <F8> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texexec.pl --autopdf --pdf --list --result=%:p:r_list %"<CR><CR>
 
 
</pre>
 
</pre>
  
Line 371: Line 343:
 
   nnoremap <silent> ]q :<c-u><c-r>=v:count1<cr>cnext<cr>zz
 
   nnoremap <silent> ]q :<c-u><c-r>=v:count1<cr>cnext<cr>zz
 
   nnoremap <silent> [q :<c-u><c-r>=v:count1<cr>cprevious<cr>zz
 
   nnoremap <silent> [q :<c-u><c-r>=v:count1<cr>cprevious<cr>zz
 +
  nnoremap <silent> ]Q :<c-u>clast<cr>zz
 +
  nnoremap <silent> [Q :<c-u>cfirst<cr>zz
 
Or install Tim Pope's <i>unimpaired</i> plugin.
 
Or install Tim Pope's <i>unimpaired</i> plugin.
  
Line 387: Line 361:
 
     rm *.aux *.bbl *.blg *.log *.tuc
 
     rm *.aux *.bbl *.blg *.log *.tuc
  
If you put these mappings to your <code>.vimrc</code> file, you can then compile the document with F9 and preview it with F8:
+
If you put these mappings to your <code>vimrc</code> file, you can then compile the document with F9 and preview it with F8:
  
 
  " map ":make" to the F9 key
 
  " map ":make" to the F9 key
Line 397: Line 371:
 
  nmap <F8> :exe "lcd" fnameescape(expand("%:p:h"))<CR>:make test<CR>
 
  nmap <F8> :exe "lcd" fnameescape(expand("%:p:h"))<CR>:make test<CR>
  
 +
== Other useful Vim plugins ==
 +
 +
=== Autocompletion ===
 +
 +
Vim offers a rich completion mechanism (<tt>:help ins-completion</tt>), but there are several plugins
 +
that improve on it, in particular, to provide automatic completion of keywords:
 +
 +
* MUcomplete[https://github.com/lifepillar/vim-mucomplete]
 +
* Coc [https://github.com/neoclide/coc.nvim].
 +
* Completor[https://github.com/maralla/completor.vim]
 +
* NeoComplete[https://github.com/Shougo/neocomplete.vim]
 +
* Deoplete (for NeoVim)[https://github.com/Shougo/deoplete.nvim]
 +
* YouCompleteMe[https://github.com/Valloric/YouCompleteMe]
 +
* AutoComplPop[https://github.com/vim-scripts/AutoComplPop]
 +
* SuperTab[https://github.com/ervandew/supertab]
 +
 +
In the asciicast at the top of this page MUcomplete was used.
 +
 +
=== UltiSnips ===
 +
 +
UltiSnips[https://github.com/SirVer/ultisnips] is a sophisticated snippets manager.
 +
Here are a few examples of useful UltiSnips snippets for ConTeXt:
 +
 +
snippet "s(tart)?" "start / stop" br
 +
\start${1:something}$2
 +
${3:${VISUAL}}
 +
\stop$1
 +
endsnippet
 +
 +
snippet enum "Enumerate" b
 +
\startitemize[n]
 +
\item ${0:${VISUAL}}
 +
\stopitemize
 +
endsnippet
 +
 +
snippet item "Itemize" b
 +
\startitemize
 +
\item
 +
${0:${VISUAL}}
 +
\stopitemize
 +
endsnippet
 +
 +
snippet it "Individual item" b
 +
\item
 +
${0:${VISUAL}}
 +
endsnippet
 +
 +
snippet fig "External figure" b
 +
\startplacefigure
 +
\externalfigure[${1:${VISUAL}}][$2]
 +
\stopplacefigure
 +
endsnippet
 +
 +
Save the above text into <tt>~/.vim/UltiSnips/context.snippets</tt>.
 +
Click on the asciicast linked at the top of this document to see UltiSnips snippets in action.
 +
 +
=== Outline of a document ===
 +
 +
Tagbar[https://github.com/majutsushi/tagbar] is a useful plugin to display an outline or a table of contents of a document.
 +
It uses Ctags, which you must install, too. Ctags does not support ConTeXt out of the box, but it is easy to extend.
 +
Create a <tt>.ctags</tt> file in your home directory, then copy and paste the following:
 +
 +
--langdef=context
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startsection<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/\. \1/s,section/
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startsubsection<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/\.\. \1/s,subsection/
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startsubsubsection<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/\.\.\. \1/s,subsubsection/
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startchapter<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/\1/c,chapter/
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startsubject<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/SUBJ \1/c,subject/
 +
--regex-context=/^<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\\startpart<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\<nowiki>[[</nowiki>^<nowiki>]]</nowiki>*title<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*=<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*\{<nowiki>[[</nowiki>:space:<nowiki>]]</nowiki>*(.+)\}/\1/p,part/
 +
 +
Put this in your <tt>vimrc</tt>:
 +
    let g:tagbar_type_context = {
 +
          \ 'ctagstype': 'context',
 +
          \ 'kinds': [
 +
          \ 'p:parts',
 +
          \ 'c:chapters',
 +
          \ 's:sections'
 +
          \ ],
 +
          \ 'sort': 0
 +
          \ }
 +
 +
That's it! See the image at the top of this document for an example.
 +
 +
=== Using LaTeX-Suite ===
 +
 +
[http://vim-latex.sourceforge.net/ latex-suite] currently doesn't support ConTeXt, but if you use it, here's what you have to do to compile ConTeXt documents:
 +
 +
1. After downloading and installing latex-suite, locate the file "texrc" (usually located in <code>~/.vim/ftplugin/latex-suite</code>). Copy this file to <code>~/.vim/ftplugin/tex/texrc</code>
 +
 +
2. Open this copy in your favorite editor (vim comes to mind...)
 +
 +
3. After line 80 in this file, there is a series of "Compiler rules." Just add this line to the section:
 +
 +
TexLet g:Tex_CompileRule_cont = 'texexec --pdf --nonstopmode $*'
 +
 +
This will add compilation for ConTeXT. In order to use it:
 +
 +
4. When you're in vim normal mode, run this command:
 +
 +
TGTarget cont [that's "colon TGTarger cont"]
 +
 +
5. Edit your TeX-files, save the changes; when you want to compile, switch to normal mode and just type <code>\ll</code> (that's '' 'backslash el el' '')
 +
 +
Voila, compilation should start. You'll have to specify this compiler target every timeI you open a TeX-file in Vim. If you want to make this the default compiler, you should have this line in your texrc:
 +
 +
TexLet g:Tex_DefaultTargetFormat = 'cont'
  
 
{{Installation navbox}}
 
{{Installation navbox}}
  
 
[[Category:Text Editors]]
 
[[Category:Text Editors]]

Revision as of 16:21, 9 April 2019

< Text Editors | Related Programs >

http://www.vim.org/

This page is about editing ConTeXt source in Vim, gVim, MacVim, NeoVim, and other Vim clones. The page describes the scripts available with Vim v8.0.0055 or later. If you are using Vim 7, see Using the scripts with an older Vim below.

Using ConTeXt in Vim

Nikolai Weibull was the first one who wrote context.vim files and submitted them to the official Vim repository. They are part of the official Vim 7, and were expanded and improved in Vim 8.

If you feel that something is missing, please contribute!

Information about providing feedback is in the header of the scripts.

Asciicast

context-in-vim.png

This asciicast[1] gives you a taste of ConTeXt editing in Vim.

Typesetting

The recommended way to typeset a ConTeXt document is to use the :ConTeXt command. Just type:

:ConTeXt

to compile the document in the current buffer. Typesetting happens in the background if you are using Vim 8.0.0047 or later, so you may continue working on your document. If there are errors, the quickfix window opens automatically to show the errors (one per line). The cursor stays in the main document, so your typing workflow is not disrupted. You may use standard quickfix commands to jump between errors: :cfirst, :cprev, :cnext, etc… (see :help quickfix). See below for useful mappings for these commands.

If your document is typeset without errors, Success! is printed at the bottom of the screen.

The :ConTeXt command accepts an optional path, in case you want to typeset a document different from the current one (useful for big projects).

You may check the status of your ConTeXt jobs with :ConTeXtJobStatus, and you may stop all running jobs with :ConTeXtStopJobs (these commands are available only in Vim 8.0.0047 or later).

An alternative way to typeset a document is to use :make. Set the current working directory to the directory of the current buffer, then execute :make:

:lcd /path/to/my/project
:make

If a Makefile exists in the working directory, it is used (see below for a sample Makefile). You may let g:context_ignore_makefile=1 to ignore an existing Makefile. If no Makefile is found, mtxrun will be invoked directly. Note that :make always performs synchronous typesetting. Also, if there are errors, the quickfix list is populated, but you have to open it manually with :copen.

It is recommended that you map the above commands. For example, you may add

 nnoremap <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>

to your vimrc, or (better)

 nnoremap <buffer> <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>

to ~/.vim/after/ftplugin/context.vim, after which pressing \tt (where \ is your leader key) will save and compile the file.

You may customize the path to the mtxrun executable by setting g:context_mtxrun. For example, if you want to use your ConTeXt Beta installation at ~/Applications/ConTeXt-Beta, and you are using macOS, you may set the variable as follows:

let g:context_mtxrun = 'PATH=$HOME/Applications/ConTeXt-Beta/tex/texmf-osx-64/bin:$PATH mtxrun'

To enable/disable SyncTeX, use \setupsynctex[state=start] and \setupsynctex[state=stop], respectively, in your document.

You may pass mtxrun additional options by putting them in g:context_extra_options. For example:

let g:context_extra_options = '--arrange --autopdf'

The --autogenerate and --nonstopmode options are always included in the command.

Finally, for each of the above variables, a corresponding buffer-local variable with the same name may be defined, whose value takes precedence over the global value.

Editing features

You may use the following commands to quickly jump to different parts of your document:

  • [[: jump to the previous start of subject, section, chapter, part, component, or product;
  • ]]: jump to the next start of subject, section, chapter, part, component, or product;
  • []: jump to the previous end of section, chapter, etc…;
  • ][: jump to the next end of section, chapter, etc…;
  • [{: jump to the previous \start… or \setup… command;
  • ]}: jump to the next \stop… or \setup… command;

Each of the above accepts an optional count. For example, you may type 3[{ to jump three \start… commands before.

You may use the following ConTeXt-specific text objects, to be used in Visual or Operator-pending mode (see :help text-objects):

  • i$: inside $…$ (dollars excluded);
  • a$: around $…$ (dollars included);
  • tp: a ConTeXt paragraph.

So, for example, you may copy (“yank” in Vim's jargon) a paragraph by typing ytp (“yank a TeX paragraph“), delete it with dtp, select it with vtp, reflow it with gqtp, etc… Similarly, you may yank a formula with vi$ (or va$), and delete it, select it, etc…, in a similar fashion.

If you have enabled the matchit plugin included in Vim (see :help matchit), you may also type % to jump between matching \start… and \stop… commands, or between matching parentheses.

You may jump to a different file by positioning the cursor over the file name and typing gf (:help gf). For example, if you have the following in your document:

\component my_component

putting the cursor over my_component and pressing gf will open my_component.tex.

Similarly, you may use [<c-i> (this is square bracket followed by ctrl-i) to jump to the definition of the word under the cursor (even if it is in a different file), or [i to display the (first line of the) definition under the status line. For these and similar commands, see :help include-search.

Vim searches for files in the locations specified by the path option. You may need to adjust the value of path for the above to work (see :help 'path').

Integration with MetaPost

Vim offers excellent support for editing METAFONT and MetaPost documents (mf and mp filetypes). Most of the features of such filetypes work also inside ConTeXt's MetaPost environments, such as \startMPpage… \stopMPpage.

In particular, Vim automatically highlights and indents MetaPost and MetaFun code inside a ConTeXt document. Besides, when you are inside a MetaPost environment, you may press CTRL-X followed by CTRL-O to complete a MetaPost/MetaFun keyword (see below for a list of several autocompletion plugins to streamline this). This works out of the box: no configuration is required. Watch the asciicast above for a demo.

Integration with other languages

Lua syntax highlighting is used inside \directlua{} and \ctxlua{} commands, and inside \startluacode… \stopluacode. XML highlighting is used inside \startXML… \stopXML.

You may embed other filetypes. Just define g:context_include (or b:context_include for buffer-specific settings). For example, if you want to highlight C++ code inside, say, \startCPP… \stopCPP, define:

let g:context_include = { 'cpp' : 'CPP' }

The key is the name of the filetype and the corresponding value is name of the command.

Using the scripts with an older Vim

If you are using an older Vim, you may copy the following scripts from Vim's distribution (https://github.com/vim/vim) into corresponding folders in your .vim folder (so, for example runtime/ftplugin/context.vim must be copied into ~/.vim/ftplugin/context.vim):

runtime/autoload/context.vim
runtime/autoload/contextcomplete.vim
runtime/compiler/context.vim
runtime/ftplugin/{context,mf,mp}.vim
runtime/indent/{context,mf,mp}.vim
runtime/syntax/{context,mf,mp}.vim

If you get the following error when you open a ConTeXt or MetaPost document:

E410: Invalid :syntax subcommand: iskeyword

edit the syntax files and remove the syn iskeyword or syntax iskeyword line. Instead, put a corresponding command in ~/.vim/after/ftplugin/context.vim:

setlocal iskeyword=@,48-57,a-z,A-Z,192-255

and in ~/.vim/after/ftplugin/mp.vim for MetaPost:

setlocal iskeyword=@,_

Everything should work, at least with Vim 7.4.

TODO

Filetype detection

TeX (Plain TeX), LaTex and ConTeXt all use the .tex extension for files, which makes it difficult to detect the filetype based on the extension. From Vim 7 onwards, Vim does some intelligent checking to see it the file is plaintex or latex or context.

If the first line of a *.tex file has the form

%&<format>

then this determines the file type: plaintex (for Plain TeX), context (for ConTeXt), or tex (for LaTeX). Otherwise, the file is searched for keywords to choose context or tex. If no keywords are found, it defaults to plaintex. You can change the default by defining the variable g:tex_flavor to the format (not the file type) you use most. Use one of these:

let g:tex_flavor = "plain"
let g:tex_flavor = "context"
let g:tex_flavor = "latex"

Currently no other formats are recognized.

  • If you use ConTeXt most of the time, but occasionally use LaTeX or Plain TeX, you can add the following to your vimrc
let g:tex_flavor = "context"
  • If you only use ConTeXt, you can add the following lines to filetype.vim:
" ConTeXt
augroup filetypedetect
	au! BufRead,BufNewFile *.tex		setfiletype context
augroup END

so the next time you open a *.tex file, Vim will always recognize it as a ConTeXt document.

Spell checking

Vim 7 or later has a built-in spell checker. To enable it or disable it, use:

:set spell

or

:set nospell

respectively. To set the language to be used for spell checking, set the spelllang option accordingly. For example:

:set spelllang=en_us

Use lowercase letters (en_us, not en_US). When you set spelllang, Vim offers to download the language data into your .vim folder, if such language is not available. You can put the above settings in your vimrc if you like.

Powerful key mappings

In the following, <leader> denotes your “leader” (:help mapleader), that is, the prefix for user-defined mappings. By default, the leader is the backslash character, but that may be changed by the user. For example, to use a comma as a leader, put this in your vimrc:

map <leader> ,

Rather than overriding the default leader, you may define an alternative key. The <space> is a good choice, because by default it has the same function as the <right> key, and it is comfortable to type:

map <space> <leader>  " Use <space> as an alternative leader (backslash can still be used)

Clean up auxiliary files

The following function can be used to clean up temporary files:

fun! ConTeXtClean()
  let l:currdir = expand("%:p:h")
  let l:tmpdirs = ['out'] " Temporary directories
  let l:suffixes = ['aux', 'bbl', 'blg', 'fls', 'log', 'tuc'] " Suffixes of temporary files
  for ff in glob(l:currdir . '/*.{' . join(l:suffixes, ',') . '}', 1, 1)
    call delete(ff)
  endfor
  for dd in l:tmpdirs
    let l:subdir = l:currdir . '/' . dd
    if isdirectory(l:subdir)
      for ff in glob(l:subdir . '/*.{' . join(l:suffixes, ',') . '}', 1, 1)
        call delete(ff)
      endfor
    endif
    call delete(l:subdir) " Delete directory (only if empty)
  endfor
  echomsg "Aux files removed"
endf

Customize l:tmpdirs and l:suffixes to suit your needs. In Windows systems, you may have to replace each slash with a backslash, too.

The following mapping allows you to remove auxiliary files by pressing \tc:

nnoremap <silent><buffer> <leader>tc :<c-u>call ConTeXtClean()<cr>

Snippets

Vim allows you to define abbreviations for frequently used pieces of text (see :help abbreviations). Here are a few examples:

fun! Eatchar(pat)  " See :help abbreviations
   let c = nr2char(getchar(0))
   return (c =~ a:pat) ? '' : c
endfun

iab <buffer> ch- \startchapter[title={<c-o>ma}]<cr><c-o>mb<cr>\stopchapter<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> s- \startsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> ss- \startsubsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsubsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> sss- \startsubsubsection[title={<c-o>ma}]<cr><c-o>mb<cr>\stopsubsubsection<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> slide- \startslide[title={<c-o>ma}]<cr><c-o>mb<cr>\stopslide<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> fig- \startplacefigure<cr><tab>\externalfigure[<c-o>ma]%<cr>[]<c-o>mb<cr><c-d>\stopplacefigure<esc>`a`b<c-o>a<c-r>=Eatchar('\s')<cr>
iab <buffer> item- \startitemize<cr><cr>\stopitemize<up><tab>\item
iab <buffer> enum- \startitemize[n]<cr><cr>\stopitemize<up><tab>\item
iab <buffer> i- \item

Type the abbreviation followed by Space to expand the snippet, then continue typing normally.

Inside the definition of an abbreviation, marks may be set (see :h m), which allow you to jump between the different parts of a snippet with TAB (CTRL-i) and CTRL-O (in Normal mode) after the abbreviation is expanded (see :h jump-motions). For example, after typing ch- , the cursor will be at the title's position. If you press <esc><tab> (or <c-o><tab> if you want to stay in Insert mode after the jump), you will jump between \startchapter and \stopchapter.

Buffer-local Insert-mode macros to speed up editing

(By D.A. 19:52, 8 Jul 2005 (CEST))

  • I have remapped <leader> to comma (one hardly ever use commas just before a letter)
  • two types of mappings: stand-alone and changing the previous word
  • usage of mappings that change the previous word: type the name of the macro and ,ta (for tag, use your leader character instead of the comma); it created \start-\stop block of the macro
  • put the code into .vim/after/plugin/context.vim
let maplocalleader = mapleader

" Make start-stop block out of the previous word
imap <buffer> <LocalLeader>ta \start<Cr>\stop<Cr><Esc>4bhdiw$pj$pO
imap <buffer> <LocalLeader>tb \begin<Cr>\end<Cr><Esc>4bhdiw$pj$pO

" Itemize
imap <buffer> <LocalLeader>it \startitemize<Cr>\stopitemize<Esc>O\item<Space>
imap <buffer> <LocalLeader>en \startitemize[n]<Cr>\stopitemize<Esc>O\item<Space>
imap <buffer> <LocalLeader>i<Return> \item<Space>

" Font switching and emphasize
imap <buffer> <LocalLeader>em {\em }<Left>
imap <buffer> <LocalLeader>sc {\sc }<Left>

" Define... and setup...
imap <buffer> <LocalLeader>de \define
imap <buffer> <LocalLeader>se \setup

" Typing and type
imap <buffer> <LocalLeader>ty \type{}<Left>
imap <buffer> <LocalLeader>typ typing<LocalLeader>ta

" Quote and quotation
imap <buffer> <LocalLeader>" \quotation{}<Left>
imap <buffer> <LocalLeader>' \quote{}<Left>

Key mappings borrowed from SciTE

If you use the stand-alone distribution for Windows/Linux. You can reset the key mapping to speed ConTeXt compiling.

Just add the following code to your _vimrc (or .vimrc file under Linux) file:

"run setup and complie, then open the result pdf file
 map <F5> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texexec.pl --autopdf --pdf '%'"<CR><CR>

"view the corresponding pdf file
map <F6> <Esc><Esc>:sil ! D:\"Program Files"\Adobe\Acrobat\Acrobat.exe %:p:r.pdf<CR><CR>

"run setup and make purge
map <F7> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texutil.pl --purge"<CR><CR>

"run setup and make list of the current file
map <F8> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texexec.pl --autopdf --pdf --list --result=%:p:r_list %"<CR><CR>

Quickfix mappings

It is useful to define mappings for quickfix commands, to be able to navigate among ConTeXt errors. For example:

 nnoremap <silent> ]q :<c-u><c-r>=v:count1<cr>cnext<cr>zz
 nnoremap <silent> [q :<c-u><c-r>=v:count1<cr>cprevious<cr>zz
 nnoremap <silent> ]Q :<c-u>clast<cr>zz
 nnoremap <silent> [Q :<c-u>cfirst<cr>zz

Or install Tim Pope's unimpaired plugin.

Makefiles

For your ConTeXt document, you can prepare a Makefile like this one (Contributed by Buggs):

# An example Makefile to compile a context file, paper.tex
paper.pdf: paper.tex
    context paper

test:
    xpdf paper.pdf

clean:
    rm *.aux *.bbl *.blg *.log *.tuc

If you put these mappings to your vimrc file, you can then compile the document with F9 and preview it with F8:

" map ":make" to the F9 key
imap <F9> <ESC>:exe "lcd" fnameescape(expand("%:p:h"))<CR>:make<CR>
nmap <F9> :exe "lcd" fnameescape(expand("%:p:h"))<CR>:make<CR>

"map ":make test" to the F8 key
imap <F8> <ESC>:exe "lcd" fnameescape(expand("%:p:h"))<CR>:make test<CR>
nmap <F8> :exe "lcd" fnameescape(expand("%:p:h"))<CR>:make test<CR>

Other useful Vim plugins

Autocompletion

Vim offers a rich completion mechanism (:help ins-completion), but there are several plugins that improve on it, in particular, to provide automatic completion of keywords:

  • MUcomplete[2]
  • Coc [3].
  • Completor[4]
  • NeoComplete[5]
  • Deoplete (for NeoVim)[6]
  • YouCompleteMe[7]
  • AutoComplPop[8]
  • SuperTab[9]

In the asciicast at the top of this page MUcomplete was used.

UltiSnips

UltiSnips[10] is a sophisticated snippets manager. Here are a few examples of useful UltiSnips snippets for ConTeXt:

snippet "s(tart)?" "start / stop" br
\start${1:something}$2
	${3:${VISUAL}}
\stop$1
endsnippet

snippet enum "Enumerate" b
\startitemize[n]
	\item ${0:${VISUAL}}
\stopitemize
endsnippet

snippet item "Itemize" b
\startitemize
	\item
	${0:${VISUAL}}
\stopitemize
endsnippet

snippet it "Individual item" b
\item
${0:${VISUAL}}
endsnippet

snippet fig "External figure" b
\startplacefigure
	\externalfigure[${1:${VISUAL}}][$2]
\stopplacefigure
endsnippet

Save the above text into ~/.vim/UltiSnips/context.snippets. Click on the asciicast linked at the top of this document to see UltiSnips snippets in action.

Outline of a document

Tagbar[11] is a useful plugin to display an outline or a table of contents of a document. It uses Ctags, which you must install, too. Ctags does not support ConTeXt out of the box, but it is easy to extend. Create a .ctags file in your home directory, then copy and paste the following:

--langdef=context
--regex-context=/^[[:space:]]*\\startsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\. \1/s,section/
--regex-context=/^[[:space:]]*\\startsubsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\.\. \1/s,subsection/
--regex-context=/^[[:space:]]*\\startsubsubsection[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\.\.\. \1/s,subsubsection/
--regex-context=/^[[:space:]]*\\startchapter[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\1/c,chapter/
--regex-context=/^[[:space:]]*\\startsubject[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/SUBJ \1/c,subject/
--regex-context=/^[[:space:]]*\\startpart[[:space:]]*\[[^]]*title[[:space:]]*=[[:space:]]*\{[[:space:]]*(.+)\}/\1/p,part/

Put this in your vimrc:

   let g:tagbar_type_context = {
         \ 'ctagstype': 'context',
         \ 'kinds': [
         \ 'p:parts',
         \ 'c:chapters',
         \ 's:sections'
         \ ],
         \ 'sort': 0
         \ }

That's it! See the image at the top of this document for an example.

Using LaTeX-Suite

latex-suite currently doesn't support ConTeXt, but if you use it, here's what you have to do to compile ConTeXt documents:

1. After downloading and installing latex-suite, locate the file "texrc" (usually located in ~/.vim/ftplugin/latex-suite). Copy this file to ~/.vim/ftplugin/tex/texrc

2. Open this copy in your favorite editor (vim comes to mind...)

3. After line 80 in this file, there is a series of "Compiler rules." Just add this line to the section:

TexLet g:Tex_CompileRule_cont = 'texexec --pdf --nonstopmode $*'

This will add compilation for ConTeXT. In order to use it:

4. When you're in vim normal mode, run this command:

TGTarget cont [that's "colon TGTarger cont"] 

5. Edit your TeX-files, save the changes; when you want to compile, switch to normal mode and just type \ll (that's 'backslash el el' )

Voila, compilation should start. You'll have to specify this compiler target every timeI you open a TeX-file in Vim. If you want to make this the default compiler, you should have this line in your texrc:

TexLet g:Tex_DefaultTargetFormat = 'cont'