【问题标题】:gvim(linux) with r-plugin not recognizing .R files with comments带有r-plugin的gvim(linux)无法识别带有注释的.R文件
【发布时间】:2012-07-30 15:08:06
【问题描述】:

注意:重写原始问题以反映给出的正确解决方案。

vim (ubuntu 12.04 w/ r-plugin) 无法将 .R 或 .r 文件识别为 R 文件,并且安装了 r-plugin 并在文件中添加注释,如下所示。

# comment
x <- 2
x

没有评论,一切正常。 我的 ~/.vim/filetype.vim 读取:

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

【问题讨论】:

  • 能否提供两个示例文件,一个有效,一个无效?
  • 感谢 Thor,在尝试为此示例创建两个等效文件后,我意识到有问题的文件以注释开头。我将添加到上面的问题以重现问题。

标签: r vim vim-plugin


【解决方案1】:

多种文件类型似乎使用R 扩展名。我在$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

所以你需要在文件中包含 cmets 以便 Vim 正确检测 R。

如果您从不使用 Rexx 或 Rebol 文件,您可以覆盖检测:

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

【讨论】:

  • 我的 ~/.vim/filetype.vim 文件当前显示为:augroup filetypedetect au! BufRead,BufNewFile *.r setfiletype r au! BufRead,BufNewFile *.R setfiletype r au! BufRead,BufNewFile *.Rnw setf noweb augroup END 那不应该解决吗?
  • 将其添加到原始问题中,以便格式显示更好。
  • 将此标记为正确,因为它解决了问题...我的实际解决方案是将“setfiletype r”更改为“setfiletype = r”,但上述方法也可以。
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 2015-12-03
  • 2013-02-04
  • 2023-03-08
  • 2015-06-29
  • 2023-01-30
相关资源
最近更新 更多