Vim Tips Wiki


1,610pages on
this wiki
Revision as of 02:14, October 10, 2013 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Use this page to discuss script 3038 noweb: syntax file with separate highlighting for documentation and code

  • Add constructive comments, bug reports, or discuss improvements (see the guideline).
  • Do not document the script here (the author should do that on
  • This page may be out of date: check the script's page above, and its release notes.

Language and Backend from filename

To set noweb_language and noweb_backend based on filename (e.g. foo.tex.cpp.nw is a C++/TeX noweb file):

au BufEnter *.*.*.nw set filetype=noweb | exe "let noweb_language = \"" . expand("%:r:e") . "\"" | exe "let noweb_backend = \"" . expand("%:r:r:e") . "\""

To allow a default backend (e.g. foo.c.nw is a C noweb file with the default—here TeX—backend):

let noweb_backend="tex"
au BufEnter *.*.nw set filetype=noweb | exe "let noweb_language = \"" . expand("%:r:e") . "\""

Or, combining these and a default language {{script_id=3038]}}:

let noweb_language="c"
let noweb_backend="tex"
au BufEnter *.nw set filetype=noweb
au BufEnter *.*.nw exe "let noweb_language = \"" . expand("%:r:e") . "\""
au BufEnter *.*.*.nw exe "let noweb_backend = \"" . expand("%:r:r:e") . "\""

NOTE: backend and language in the filename are unmodified for noweb_backend and noweb_language, which in turn (currently) uses them unmodified to create filenames syntax/noweb_backend.vim and syntax/noweb_langauge.vim, so to use the above unmodified in your .vimrc, name files to match the vim syntax file names (rather than the language's typical extension if it differs)--it might be better to create special cases; for example, to use syntax/perl.vim for *.pl.nw (and *.*.pl.nw) files, add the following after the code listed above.

au BufEnter *.pl.nw let noweb_language="perl"

There may be a way to make this better match Vim's filetype detection—perhaps by actually using it with a temporary buffer.


Instead of "^@­­­­­$" for detecting end of code chunks I suggest you use "^@\($\| \)" (that is, @ at beginning of line followed by either end of line or space character). That is how beginning of comment blocks is defined in the noweb man page.  %def's, for example, are written in the same line as the @ that ends the code chunk.

String variable declaration (in the .vimrc file) does not need escapes for the double quotes (VIM 7.3), so the correct variable declarations would be:

let noweb_backend = "tex"
let noweb_language = "cpp"

noweb syntax with vim-latexsuite

If used as directed, the noweb filetype prevents latex-suite from being loaded. However it is possible to have different filetype and syntax like this:

autocmd BufRead,BufNewFile *.Rnw set filetype=tex syntax=noweb

Note that the order in which you set filetype and syntax is important.

Around Wikia's network

Random Wiki