[Tex/LaTex] LaTeX Suite Autocompletion Feature Not working With Rnw file

editorssweavevim

I have been a great fan of Vim with its awesome Vim-latex suite providing extremely useful keybindings and autocompletion features. Then I installed vim-r-plugin and opened a .Rnw Sweave file. While syntax highlighting and other R related features such as key shortcuts are working fine, LaTeX shortcuts no longer works. For example, if I type SSE it longer gives \section{}. But if I instead I open a .tex file, those shortcuts works just fine. I am using gVim 7.3 in windows 7. Any suggestions would be highly appreciated.

Best Answer

You need to associate the .Rnw files with tex.

The most direct way to do this is to issue the following command with the open .Rnw file:

:set filetype=tex

As soon as you have done that the vim-latex behaviour you are used to should be back. To incorporate this in a nice way in your vimrc is described here in a stackoverflow question. To wit:

autocmd BufRead,BufNewFile *.Rnw set filetype=tex

Plugins are associated with a filetype, and as far as I know a file can have only one filetype at any particular time (otherwise you could have clashes between the various plugin functionalities, UltiSnips would not know which snipps to offer etc.).

You can do this by hand of course, but if you want to avoid having to set filetype= every time you want to switch between the plugins associated with a filetype, you can use the following script in your .vimrc:

"------------------------------------
" Toggle filetypes R, Rnw and Tex 
"------------------------------------
let b:myFileType=-1   
let g:myFileTypeList=["tex","rnoweb","r"]
function! ToggleFileType()
  let b:myFileType=b:myFileType+1
  if b:myFileType>=len(g:myFileTypeList) | let b:myFileType=0 | endif
  execute "set filetype=".get(g:myFileTypeList, b:myFileType)
  echo "Filetype set to:" g:myFileTypeList[b:myFileType]
endfunction
nmap <silent> <F12> :call ToggleFileType()<CR>

You can set your own keyboard shortcut of course, by changing <F12> to something that works for you.