From ConTeXt wiki
Jump to: navigation, search

< Text Editors | Related Programs >

This page will be about editing ConTeXt source in (g)vim.


Filetype detection

Tex (plain tex), latex and context all use .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


then this determined 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.

let g:tex_flavor = "context"
" ConTeXt
augroup filetypedetect
	au! BufRead,BufNewFile *.tex		setfiletype context
augroup END

so the next time you open a *.tex file, vim will recognise always recognize it as a context file.


Vim 7 has a built-in spell checker, but enabling the spell-checker has been "forgotten" in context.vim. Either say

:syn spell toplevel

after opening the document or add

syn spell toplevel

somewhere on the top of runtime/syntax/context.vim, as in:

runtime! syntax/plaintex.vim

let s:cpo_save = &cpo
set cpo&vim

syn spell toplevel

Additionally, you have to set

:set spell spelllang=en_us

or the language of your preference once you have installed it into vim. Use lowercase letters (en_us not en_US). You can put this line in your .vimrc if you like.

ftplugin and syntax file

Nikolai Weibull was the first one who wrote context.vim files and submitted them to the official vim cvs repository. They are part of the official Vim 7.

If you are using an older Vim, you can download the files at:

It's mostly up to you which syntax highlighting scheme you prefer. context.vim is a lighter version than tex.vim. It knows some ConTeXt details that are not implemented in tex.vim, while math and some other TeX features are not supported yet in context.vim.

If you feel that something is missing, please contribute!


compiler plugin

Context complier plugin for vim is available from

After installing this, you can compile a TeX file using

 :make %

which will also highlight the error messages. If you prefer keymappings, you can add

 nmap <F5> :make %<cr>

to your .vimrc or your ftplugin/context.vim, after which pressing <F5> will compile the file.

Other useful vim plugins

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' 

Powerful key mappings

a set of buffer-local insert-mode macros to speed up ConTeXt source editing (by D.A. 19:52, 8 Jul 2005 (CEST))

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 --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 --purge"<CR><CR>

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


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
    texexec paper

    xpdf paper.pdf

    rm *.bbl *.dvi *.aux *.log *.blg

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

" map ":make" to the F9 key
imap <F9> <ESC>:make<CR>
nmap <F9> :make<CR>

"map ":make test" to the F8 key
imap <F8> <ESC>:make test<CR>
nmap <F8> :make test<CR>

MetaPost extension

Should highlight the syntax between btex/verbatimtex ... etex as TeX.

This doesn't work 100% (problems with commands inside comments): If anyone knows how to repair that, please do so. It would be nice if this would have landed in the official metapost syntax highlighting script for vim one day.

unlet b:current_syntax
syn include @texTop syntax/tex.vim
" MetaPost has TeX inserts for typeset labels
" verbatimtex, btex, and etex will be treated as keywords
syn match mpTeXbegin "\(verbatimtex\|btex\)"
syn match mpTeXend "etex"
syn region mpTeXinsert start="\(verbatimtex\|btex\)"hs=e+1 end="etex"he=s-1 \
                       contains=@texTop,mpTeXbegin,mpTeXend containedin=ALL keepend


Personal tools
External Help