Vim

From Wiki
Revision as of 12:45, 30 October 2016 by Lifepillar (talk | contribs)
Jump to navigation Jump to search

< Text Editors | Related Programs >

http://www.vim.org/

This page is about editing ConTeXt source in Vim, gVim, MacVim, NeoVim, and other Vim clones. Some of the described features are available only in Vim v8.0.0047 or later: they will be noted below.

A note on 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.

A note on 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.

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.

Typesetting

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

:ConTeXt

to typeset 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 error 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). It is useful to add mappings for these commands. 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 unimpaired plugin.

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. Make sure that the current working directory is set to the path to the file you want to compile (set with :lcd …), and type:

:make

If a Makefile is present in the working directory, it will be used (you may let g:context_ignore_makefile=1 to ignore it). Otherwise, 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 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 (if necessary) 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'

You may decide whether to use synctex or not by setting the g:context_synctex flag to 1 or 0, respectively. For example:

let g:context_synctex = 1

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, --nonstopmode and --synctex 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 start of the previous subject/section/chapter/part/component/product;
  • ]]: jump to the start of the next subject/section/chapter/part/component/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, if you type 3[{ you will 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 and so on. In particular, you may reflow a paragraph with gqtp (see :help gq). 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 the start and the stop of a command, or between 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.

Vim searches for files in the locations specified by the path option (this is a Vim feature). So, you may have to set path for the above to work.


Other useful vim plugins

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 http://vim-latex.sourceforge.net/?

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))

  • 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>

Makefile

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

test:
    xpdf paper.pdf

clean:
    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