Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
The thread that you suggested is relevant for those people who can't get latex-suite to work at all with .Rnw files. In my case, my latex suite is loaded with .Rnw files with all the menu and stuff. I can get the short cuts work with F5, F7 and alt keys. It's just that short cuts do not work in the insert mode. It's bugging me a lot and I have been on the hunt for a solution the whole day but could not find a fix yet. –  user1789451 Aug 28 '13 at 19:04
    
Maybe I should have been more explicit in my answer then. If you try :set filetype=tex when you are editing the Rnw file you should see the behaviour come back. I have just tested it and it works fine on my machine. Maybe there are some .vimrc settings in play but i doubt that. –  FvD Aug 28 '13 at 19:41
    
:set filetype=tex works just great. Thanks a lot. That was a life-saver! Now, should I do this every time I edit a Rnw file or I can put this in some other forms in vimrc. I put that command naively in the vimrc and it did not work. It's not a big deal to set it up manually everytime but I guess there must be something to automate it. Thanks again. –  user1789451 Aug 28 '13 at 19:51
    
@Rushad I'm happy it works for you. If you add: autocmd BufRead,BufNewFile *.Rnw set filetype=tex to your .vimrc you should never have to think about it again. –  FvD Aug 28 '13 at 20:09
    
It works like a charm. That was such a great help. Can't thank enough! –  user1789451 Aug 28 '13 at 20:17
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.