Sweave Syntax Highlighting in vim
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
If you use Sweave and vim, you might have wondered if it’s possible to have it syntax highlight Rnw/Snw files properly : the latex bits with LaTeX syntax and the noweb R chunks with R syntax. It is! Take a look a the screenshot below:
In order to have this working, you only need to properly configure your vim. First, edit your ~/.vim/filetype.vim, adding the following lines to it:
1 2 3 4 5 | augroup filetypedetect au! BufRead,BufNewFile *.r setfiletype r au! BufRead,BufNewFile *.R setfiletype r au! BufRead,BufNewFile *.Rnw setf noweb augroup END |
Then, create a file named ~/.vim/after/syntax/noweb.vim, (create the directories with mkdir, if needed), with the following content:
1 2 3 4 5 6 7 8 9 | runtime! syntax/tex.vim unlet b:current_syntax syntax include @nowebR syntax/r.vim syntax region nowebChunk start="^<<.*>>=" end="^@" contains=@nowebR syntax region Sexpr start="\\Sexpr{" end="}" keepend hi Sexpr gui=bold guifg=chocolate2 let b:current_syntax="noweb" |
For users of Windows (contributed by Gabor Grothendieck)
If you’re running vim under Windows, the directory structure might difer somewhat from the above. On Windows (assuming gvim 6.3), the %VIM%\gvim63 directory should contain:
1 2 3 4 | vim.exe gvim.exe filetype.vim syntax\noweb.vim |
You should just do the alterations indicated above but you should save the files according to this structure.
That’s it, next time you open a file with extension .Rnw, vim will properly syntax highlight it.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.