Difference between revisions of "Vim"

From Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
http://www.vim.org/
 
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.  
+
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.  
  
== Filetype detection ==
+
== A note on filetype detection ==
TeX (Plain TeX), LaTex and ConTeXt all use the <code>.tex</code> 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 <code>plaintex</code> or <code>latex</code> or <code>context</code>.
 
  
If the first line of a <code>*.tex</code> file has the form  
+
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>.
 +
 
 +
If the first line of a <tt>*.tex</tt> file has the form  
 
  %&<format>
 
  %&<format>
then this determines the file type:  <code>plaintex</code> (for Plain TeX), <code>context</code> (for
+
then this determines the file type:  <tt>plaintex</tt> (for Plain TeX), <tt>context</tt> (for
ConTeXt), or <code>tex</code> (for LaTeX).  Otherwise, the file is searched for keywords to
+
ConTeXt), or <tt>tex</tt> (for LaTeX).  Otherwise, the file is searched for keywords to
choose <code>context</code> or <code>tex</code>.  If no keywords are found, it defaults to <code>plaintex</code>.
+
choose <tt>context</tt> or <tt>tex</tt>.  If no keywords are found, it defaults to <tt>plaintex</tt>.
You can change the default by defining the variable <code>g:tex_flavor</code> to the format
+
You can change the default by defining the variable <tt>g:tex_flavor</tt> to the format
 
(not the file type) you use most.  Use one of these:  
 
(not the file type) you use most.  Use one of these:  
 
  let g:tex_flavor = "plain"
 
  let g:tex_flavor = "plain"
Line 20: Line 21:
 
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 <code>.vimrc</code>
+
* 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 <code>filetype.vim</code>:
+
* If you only use ConTeXt, you can add the following lines to <tt>filetype.vim</tt>:
 
  " ConTeXt
 
  " ConTeXt
 
  augroup filetypedetect
 
  augroup filetypedetect
 
  au! BufRead,BufNewFile *.tex setfiletype context
 
  au! BufRead,BufNewFile *.tex setfiletype context
 
  augroup END
 
  augroup END
so the next time you open a <code>*.tex</code> 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.
  
 
== Spell-checker ==
 
== Spell-checker ==
  
Vim 7 has a built-in spell checker. To enable Vim's spell checking:
+
Vim 7 or later has a built-in spell checker. To enable it or disable it, use:
 
  :set spell
 
  :set spell
To set the language to be used for spell checking, set the `spelllang` option accordingly. For example:
+
or
 +
:set nospell
 +
respectively.
 +
To set the language to be used for spell checking, set the <tt>spelllang</tt> option accordingly. For example:
 
  :set spelllang=en_us
 
  :set spelllang=en_us
Use lowercase letters (<code>en_us</code>, not <code>en_US</code>).
+
Use lowercase letters (<tt>en_us</tt>, not <tt>en_US</tt>).
You can put such lines in your <code>.vimrc</code> if you like.
+
You can put such lines in your <tt>.vimrc</tt> if you like.
  
== ftplugin and syntax file ==
+
== Using ConTeXt in Vim ==
  
Nikolai Weibull was the first one who wrote <tt>context.vim</tt> files and submitted them to the official vim cvs repository.
+
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.  
+
They are part of the official Vim 7, and were expanded and improved in Vim 8.
  
If you are using an older Vim, you can download the files at:
+
<b>If you feel that something is missing, please contribute!</b>
* [http://cvs.sourceforge.net/viewcvs.py/vim/vim7/runtime/syntax/context.vim runtime/syntax/context.vim] - syntax highlighting
 
* [http://cvs.sourceforge.net/viewcvs.py/vim/vim7/runtime/ftplugin/context.vim runtime/ftplugin/context.vim] - filetype specific behaviour (like repeating comments at the beginning of line)
 
  
 +
Information about providing feedback is in the header of the source scripts.
  
It's mostly up to you which syntax highlighting scheme you prefer. <code>context.vim</code> is a lighter version than <code>tex.vim</code>. It knows some ConTeXt details that are not implemented in <code>tex.vim</code>, while math and some other TeX features are not supported yet in <code>context.vim</code>.
+
=== Typesetting ===
  
<b>If you feel that something is missing, please contribute!</b>
+
The recommended way to typeset a ConTeXt document is to use the <tt>ConTeXt</tt> 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: <tt>:cfirst</tt>, <tt>:cprev</tt>,
 +
<tt>:cnext</tt>, etc… (see <tt>:help quickfix</tt>). 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.
  
=== TODO ===
+
If your document is typeset without errors, a <tt>Success!</tt> message appears.
  
* extract data from texweb and create syntax highlighting definitions for ConTeXt
+
The <tt>:ConTeXt</tt> command accepts an optional path, in case you want to typeset a document different
* add metafun macros (from metafun manual)
+
from the current one (useful for big projects).
* enable metapost/metafun, JavaScript and XML inside 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/?
 
* vim auto-complete for vim
 
  
== compiler plugin ==
+
You may check the status of your ConTeXt jobs with <tt>:ConTeXtJobStatus</tt>, and you may stop all
 +
running jobs with <tt>:ConTeXtStopJobs</tt>.
  
Context complier plugin for vim is available from http://www.vim.org/scripts/script.php?script_id=1925
+
An alternative way to typeset a document is to use <tt>:make</tt>. ''Make sure that the current working
 +
directory is set to the path to the file you want to compile'' (set with <tt>:lcd …</tt>), and type:
 +
:make
 +
If a <tt>Makefile</tt> is present in the working directory, it will be used. Otherwise, <tt>mtxrun</tt> will
 +
be invoked directly. Note that <tt>:make</tt> always performs synchronous typesetting.
  
After installing this, you can compile a TeX file using
+
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 <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 (if necessary) and compile the file.
  
  :make %
+
You may customize the path to the <tt>mtxrun</tt> executable by setting <tt>g:context_mtxrun</tt>.
 +
For example, if you want to use your ConTeXt Beta installation at <tt>~/Applications/ConTeXt-Beta</tt>,
 +
you may set the variable as follows:
 +
let g:context_mtxrun = 'PATH=$HOME/Applications/ConTeXt-Beta/tex/texmf-osx-64/bin:$PATH mtxrun'
  
which will also highlight the error messages. If you prefer keymappings, you can add
+
TODO: <tt>g:context_synctex</tt>
  
  nmap <F5> :make %<cr>
+
Finally, you may pass <tt>mtxrun</tt> additional options by putting them in <tt>g:context_extra_options</tt>.
  
to your <code>.vimrc</code> or your <code>ftplugin/context.vim</code>, after which pressing <code><F5></code> will compile the file.
+
For each of the above variable, a corresponding buffer-local variable exists, whose value, if defined, take precedence
 +
over the global value.
  
 
== Other useful vim plugins ==
 
== Other useful vim plugins ==
Line 80: Line 100:
 
* autocomplete: http://vim.sourceforge.net/scripts/script.php?script_id=182, almost undocumented, but life-saving
 
* autocomplete: http://vim.sourceforge.net/scripts/script.php?script_id=182, almost undocumented, but life-saving
 
* spell-checker: http://www.vim.org/scripts/script.php?script_id=499, but native spell-checking support is included in Vim 7 (see above).
 
* spell-checker: http://www.vim.org/scripts/script.php?script_id=499, but native spell-checking support is included in Vim 7 (see above).
 +
 +
=== 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 ==
 
== using latex-suite ==

Revision as of 11:32, 30 October 2016

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

Spell-checker

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). You can put such lines 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 source 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, a Success! message appears.

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.

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. Otherwise, mtxrun will be invoked directly. Note that :make always performs synchronous typesetting.

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, you may set the variable as follows:

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

TODO: g:context_synctex

Finally, you may pass mtxrun additional options by putting them in g:context_extra_options.

For each of the above variable, a corresponding buffer-local variable exists, whose value, if defined, take precedence over the global value.

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