Difference between revisions of "Vim"

From Wiki
Jump to navigation Jump to search
Line 30: Line 30:
 
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.
  
== Spell-checker ==
+
== A note on 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 39: Line 39:
 
To set the language to be used for spell checking, set the <tt>spelllang</tt> option accordingly. For example:
 
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 (<tt>en_us</tt>, not <tt>en_US</tt>).
+
Use lowercase letters (<tt>en_us</tt>, not <tt>en_US</tt>). When you set <tt>spelllang</tt>, Vim offers to
You can put such lines in your <tt>.vimrc</tt> if you like.
+
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.
  
 
== Using ConTeXt in Vim ==
 
== Using ConTeXt in Vim ==
Line 49: Line 50:
 
<b>If you feel that something is missing, please contribute!</b>
 
<b>If you feel that something is missing, please contribute!</b>
  
Information about providing feedback is in the header of the source scripts.
+
Information about providing feedback is in the header of the scripts.
  
 
=== Typesetting ===
 
=== Typesetting ===
  
The recommended way to typeset a ConTeXt document is to use the <tt>ConTeXt</tt> command. Just type:
+
The recommended way to typeset a ConTeXt document is to use the <tt>:ConTeXt</tt> command. Just type:
 
  :ConTeXt
 
  :ConTeXt
 
to typeset the document in the current buffer. Typesetting happens in the background if you are using Vim 8.0.0047 or later,
 
to typeset the document in the current buffer. Typesetting happens in the background if you are using Vim 8.0.0047 or later,
Line 62: Line 63:
 
   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
Or install Tim Pope's unimpaired plugin.
+
Or install Tim Pope's <i>unimpaired</i> plugin.
  
If your document is typeset without errors, a <tt>Success!</tt> message appears.
+
If your document is typeset without errors, <tt>Success!</tt> is printed at the bottom of the screen.
  
 
The <tt>:ConTeXt</tt> command accepts an optional path, in case you want to typeset a document different
 
The <tt>:ConTeXt</tt> command accepts an optional path, in case you want to typeset a document different
Line 70: Line 71:
  
 
You may check the status of your ConTeXt jobs with <tt>:ConTeXtJobStatus</tt>, and you may stop all
 
You may check the status of your ConTeXt jobs with <tt>:ConTeXtJobStatus</tt>, and you may stop all
running jobs with <tt>:ConTeXtStopJobs</tt>.
+
running jobs with <tt>:ConTeXtStopJobs</tt> (these commands are available only in Vim 8.0.0047 or later).
  
An alternative way to typeset a document is to use <tt>:make</tt>. ''Make sure that the current working
+
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:
+
directory is set to the path to the file you want to compile' (set with <tt>:lcd …</tt>), and type:
 
  :make
 
  :make
 
If a <tt>Makefile</tt> is present in the working directory, it will be used. Otherwise, <tt>mtxrun</tt> will
 
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.
+
be invoked directly. 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>.
  
 
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
Line 82: Line 84:
 
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) will save (if necessary) and compile the file.
+
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.
  
 
You may customize the path to the <tt>mtxrun</tt> executable by setting <tt>g:context_mtxrun</tt>.
 
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>,
 
For example, if you want to use your ConTeXt Beta installation at <tt>~/Applications/ConTeXt-Beta</tt>,
you may set the variable as follows:
+
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'
 
  let g:context_mtxrun = 'PATH=$HOME/Applications/ConTeXt-Beta/tex/texmf-osx-64/bin:$PATH mtxrun'
  
TODO: <tt>g:context_synctex</tt>
+
You may decide whether to use synctec by setting the <tt>g:context_synctex</tt> flag to <tt>1</tt> or <tt>0</tt>.
 +
For example:
 +
let g:context_synctex = 1
  
 
Finally, you may pass <tt>mtxrun</tt> additional options by putting them in <tt>g:context_extra_options</tt>.
 
Finally, 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 the
 +
command.
  
For each of the above variable, a corresponding buffer-local variable exists, whose value, if defined, take precedence
+
For each of the above variables, a corresponding buffer-local variable with the same name exists, whose value,
over the global value.
+
if defined, take precedence over the global value.
  
 
== Other useful vim plugins ==
 
== Other useful vim plugins ==

Revision as of 12:08, 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.

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. 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 synctec by setting the g:context_synctex flag to 1 or 0. For example:

let g:context_synctex = 1

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

For each of the above variables, a corresponding buffer-local variable with the same name 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