JimmyT
JimmyT

Reputation: 1149

gvim(linux) with r-plugin not recognizing .R files with comments

Note: rewrote the original question to reflect the proper solution given.

vim (ubuntu 12.04 w/ r-plugin) doesn't recognize .R or .r files as R files with the r-plugin installed with a comment in the file as shown below.

# comment
x <- 2
x

Without the comment, everything works fine. my ~/.vim/filetype.vim reads:

augroup filetypedetect
  au! BufRead,BufNewFile *.r         setfiletype r
  au! BufRead,BufNewFile *.R         setfiletype r
  au! BufRead,BufNewFile *.Rnw       setf noweb
augroup END

Upvotes: 1

Views: 294

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172668

Multiple file types seem to use the R extension. I found this in $VIMRUNTIME/filetype.vim:

" Rexx, Rebol or R
au BufNewFile,BufRead *.r,*.R           call s:FTr()

func! s:FTr()
let max = line("$") > 50 ? 50 : line("$")

for n in range(1, max)
    " Rebol is easy to recognize, check for that first
    if getline(n) =~? '\<REBOL\>'
    setf rebol
    return
    endif
endfor

for n in range(1, max)
    " R has # comments
    if getline(n) =~ '^\s*#'
    setf r
    return
    endif
    " Rexx has /* comments */
    if getline(n) =~ '^\s*/\*'
    setf rexx
    return
    endif
endfor

" Nothing recognized, assume Rexx
setf rexx
endfunc

So you need to have comments in the file for Vim to properly detect R.

In case you never use Rexx or Rebol files, you can override the detection:

au BufNewFile,BufRead *.r,*.R setf r

Upvotes: 1

Related Questions