Changes

Jump to navigation Jump to search
4,720 bytes added ,  06:57, 31 July 2023
m
< [[Text Editors]] | [[Related Programs]] >
httphttps://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 in Vim v8.0.0047 0055 or later.If you are using an older Vim7, you may copy see''Using the <tt>context.vim</tt> scripts from with an older Vim's distribution ' below.(https://github.com/vim/vim) under the <tt>runtime</tt> directory, into correspondingfolders in your <ttb>.vim</tt> folder (soIf you feel that something is missing, for example <tt>ftplugin/context.vimplease contribute!</ttb> must be copied into<tt>~/.vim/ftplugin/context.vim</tt>).Everything should work, at least with Vim 7.4Information about providing feedback is in the header of the scripts.
== Using ConTeXt in Vim ==
Nikolai Weibull was the first one who wrote <tt>context.vim</tt> 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. Starting with Vim 9.0.0218, the scripts supporting ConTeXt in Vim have been rewritten in Vim 9 script (the new Vim's scripting language). For the complete documentation, see <btt>If you feel that something is missing, please contribute!:help ft-context</bttInformation about providing feedback is in the header of the scripts.
=== Asciicast ===
The recommended way to typeset a ConTeXt document is to use the <tt>:ConTeXt</tt> 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 will open automaticallyto show the errors (one per line). The cursor stays will stay in the main document, so your typing workflow is will notbe disrupted. You may use standard quickfix commands to jump between errors: <tt>:cfirst</tt>, <tt>:cprev</tt>,
<tt>:cnext</tt>, etc… (see <tt>:help quickfix</tt>). See below for useful mappings for these commands.
You may check the status of your ConTeXt jobs with <tt>:ConTeXtJobStatus</tt>, and you may stop all
running jobs with <tt>:ConTeXtStopJobs</tt> . === Setting a custom typesetting engine === The <tt>:ConTeXt</tt> command invokes the <tt>mtxrun</tt> script that is found in <tt>$PATH</tt>. For more fine grained control over the command and its environment, you may invoke `context.Typeset()` directly (or `context#Typeset(these commands are available only )` from legacy Vim script). For instance, if you have installed a version of ConTeXt in <tt>$HOME/context</tt> (where <tt>$HOME</tt> is the path to your home directory), you may define a function to use it similar to the following (you may put the following code in Vim 8<tt>~/.vim/after/ftplugin/context.vim</tt>, creating the file and the directories if they do not exist):  import autoload 'context.vim'  def MyConTeXt() const env = {'PATH': printf("%s/context/tex/texmf-<os>-<arch>/bin:%s", $HOME, $PATH)} context.Typeset("%", env) enddef and perhaps use it with a mapping:  nnoremap <silent><buffer><leader>t <scriptcmd>MyConTeXt()<cr> <tt>context.Typeset()</tt> accepts a third optional argument to specify a customtypesetting command. That must be a function that takes a path and returns thecommand as a List.0For example:  def ConTeXtCustomCommand(path: string): list<string> return ['mtxrun', '--script', 'context', '--nonstopmode', path] enddef  context.0047 or laterConTeXtTypeset("%", v:none, ConTeXtCustomCommand=== Working with large projects === Large projects are often organized as a root document and various chapterfiles. When editing a chapter file, it is convenient to invoke <tt>:ConTeXt</tt>directly on it, rather than having to switch to the root file. A “magic line”can be added at the beginning of each chapter file, which specifies therelative path to the root file. For instance:  % !TEX root = ../MyRoot.tex
An alternative way to typeset a document is to use <tt>:make</tt>. Set Vim searches for the current workingdirectory to magic line in the directory first ten lines of the current buffer, then execute <tt>:make</tt>: lcd /path/to/my/project :makeIf a <tt>Makefile</tt> exists in if the working directory, it is used (see below for a sample Makefile).You may <tt>let g:context_ignore_makefile=1</tt> to ignore an existing Makefile.If no Makefile magic line is found, <tt>mtxrun</tt> will be invoked directly.Note the document specified by that <tt>:make</tt> always performs synchronous typesetting. Also, if there areline is typeset rather than theerrors, one in the quickfix list is populated, but you current buffer. The root document does not have open it manually with <tt>:copen</tt>to be opened inVim.
It is recommended that you map === Updating the above commands. For example, you may add nnoremap <silent> <leader>tt :<c-u>update<cr>:ConTeXt<cr>to your <tt>.vimrc</tt>, or (better) 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)will save and compile the file.syntax files ===
You may customize the path to the Vim includes syntax files generated by <tt>mtxrun</tt> executable by setting <tt>g:context_mtxrun</tt>.For example, if If you want to use more up-to-date files, overriding those distributed with Vim, you may proceed as follows. Assuming your ConTeXt Beta installation at Vim configuration lives in <tt>~/Applications/ConTeXt-Beta.vim</tt>,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/bintype:$PATH mtxrun'
You may enable synctex by setting <tt>g:context_synctex< mkdir -p ~/.vim/syntax/shared cd ~/tt> to <tt>1<.vim/tt> (the default is <tt>0<syntax/tt>):shared let g:context_synctex = 1mtxrun --script interface --vim
You may pass <tt>mtxrun</tt> additional options by putting them in <tt>g:context_extra_options</tt>.For example: let g:context_extra_options = '--arrange --autopdf'The <tt>--autogenerate</tt>, <tt>--nonstopmode</tt> and <tt>--synctex</tt> options are always included in last command will create thecommand.following syntax files:
Finally, for each of the above variables, a corresponding buffer* <tt>context-local variable with the same namedata-context.vim</tt>;may be defined, whose value takes precedence over the global value* <tt>context-data-interfaces.vim</tt>;* <tt>context-data-metafun.vim</tt>;* <tt>context-data-tex.vim</tt>.
=== Editing features ===
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>).
 
=== Integration with MetaPost ===
Vim offers excellent support for editing METAFONT and MetaPost documents (<tt>mf</tt> and <tt>mp</tt> filetypes).
See <tt>:help ft-metapost</tt> for the details.
Most of the features of such filetypes work also inside ConTeXt's MetaPost environments,
such as <tt>\startMPpage… \stopMPpage</tt>.
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 ===
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 <tt>.vim</tt> folder (so, for example <tt>runtime/ftplugin/context.vim</tt> must be copied into
<tt>~/.vim/ftplugin/context.vim</tt>):
 
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
 
<b>Note:</b> the runtime scripts in Vim 9.0.0218 or later are written in Vim 9 script (the new scripting language embedded in Vim) and there is no guarantee that they will work with older versions of 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 <tt>syn iskeyword</tt> or <tt>syntax iskeyword</tt> line.
Instead, put a corresponding command in <tt>~/.vim/after/ftplugin/context.vim</tt>:
 
setlocal iskeyword=@,48-57,a-z,A-Z,192-255
 
and in <tt>~/.vim/after/ftplugin/mp.vim</tt> for MetaPost:
 
setlocal iskeyword=@,_
 
Everything should work, at least with Vim 7.4.
=== TODO ===
* Extract data from texweb and create syntax highlighting definitions for ConTeXt.
* Some essential math support.
* 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/?
== A note on filetype 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>.
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>
let g:tex_flavor = "context"
* If you only use ConTeXt, you can add the following lines to <tt>filetype.vim</tt>:
so the next time you open a <tt>*.tex</tt> file, Vim will always recognize it as a ConTeXt document.
== A note on spell Spell checking ==
Vim 7 or later has a built-in spell checker. To enable it or disable it, use:
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.
You can put the above settings in your <tt>vimrc</tt> if you like. == Powerful key mappings == In the following, <tt><leader></tt> denotes your “leader” (<tt>:help mapleader</tt>), 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 <tt>vimrc</tt> : map <leader> ,Rather than overriding the default leader, you may define an alternative key. The <tt><space></tt> is a goodchoice, 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" endfCustomize <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) ? '' : cendfun 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>\itemiab <buffer> enum- \startitemize[n]<cr><cr>\stopitemize<up><tab>\itemiab <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 differentparts 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 === (By [[User:David antos|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 <code>.vim/after/plugin/context.vim</code><pre>let maplocalleader = mapleader " Make start-stop block out of the previous wordimap <buffer> <LocalLeader>ta \start<Cr>\stop<Cr><Esc>4bhdiw$pj$pOimap <buffer> <LocalLeader>tb \begin<Cr>\end<Cr><Esc>4bhdiw$pj$pO " Itemizeimap <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 emphasizeimap <buffer> <LocalLeader>em {\em }<Left>imap <buffer> <LocalLeader>sc {\sc }<Left> " Define... and setup...imap <buffer> <LocalLeader>de \defineimap <buffer> <LocalLeader>se \setup " Typing and typeimap <buffer> <LocalLeader>ty \type{}<Left>imap <buffer> <LocalLeader>typ typing<LocalLeader>ta " Quote and quotationimap <buffer> <LocalLeader>" \quotation{}<Left>imap <buffer> <LocalLeader>' \quote{}<Left></pre> === 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 <tt>_vimrc</tt> (or <tt>.vimrc</tt> file under Linux) file:<pre>"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 filemap <F6> <Esc><Esc>:sil ! D:\"Program Files"\Adobe\Acrobat\Acrobat.exe %:p:r.pdf<CR><CR> "run setup and make purgemap <F7> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texutil.pl --purge"<CR><CR> "run setup and make list of the current filemap <F8> <Esc><Esc>:sil ! "D:\context\tex\setuptex.bat && texmfstart texexec.pl --autopdf --pdf --list --result=%:p:r_list %"<CR><CR></pre> === 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>zzOr install Tim Pope's <i>unimpaired</i> plugin. == Makefiles == For your ConTeXt document, you can prepare a Makefile likethis one (Contributed by [[User:Buggs|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 <code>vimrc</code> 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> Note that if you use <tt>:make</tt> typesetting will happen synchronously.
== Other useful Vim plugins ==
that improve on it, in particular, to provide automatic completion of keywords:
* µcompleteMUcomplete[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]
* SuperTab[https://github.com/ervandew/supertab]
The In the asciicast at the top of this page uses µcompleteMUcomplete was used.
=== Snippets UltiSnips ===
Vim allows you to define abbreviations for frequently used pieces of text (see <tt>:help abbreviations</tt>).If you need more sophisticated support for snippets of code, UltiSnips[https://github.com/SirVer/ultisnips]is the plugin to usea sophisticated snippets manager. Here are a few examples of useful UltiSnips snippets for ConTeXt:
snippet "s(tart)?" "start / stop" br
TexLet g:Tex_DefaultTargetFormat = 'cont'
== Powerful key mappings ==
 
In the following, <tt><leader></tt> denotes your “leader” (<tt>:help mapleader</tt>), 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 <tt>.vimrc</tt>:
map <leader> ,
 
=== Buffer-local Insert-mode macros to speed up editing ===
 
(By [[User:David antos|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 <code>.vim/after/plugin/context.vim</code>
<pre>
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>
</pre>
 
=== 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 <tt>_vimrc</t>> (or <tt>.vimrc</tt> file under Linux) file:
<pre>
"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>
 
</pre>
 
=== 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
Or install Tim Pope's <i>unimpaired</i> plugin.
 
== Makefiles ==
 
For your ConTeXt document, you can prepare a Makefile like this one (Contributed by [[User:Buggs|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 <code>.vimrc</code> 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>
 
 
{{Installation navbox}}
[[Category:Text Editors]]

Navigation menu