Vim:文件类型插件设置本地选项,注释和注释字符串未显示在缓冲区中

huangapple go评论56阅读模式
英文:

Vim: Filetype plugin setlocal options for comments and commentstring not appearing in buffer

问题

我正在使用Windows 10上的Vim 9.0。

我已经创建了一个新的文件类型称为'projects',但是设置本地注释选项不起作用。以下是我所做的:

  1. 我在我的vimrc文件中添加了以下行::filetype plugin indent on

  2. :filetype 命令返回:filetype detection:ON plugin:ON indent:ON

  3. 然后,我在一个新的目录ftdetect中的runtimepath的第一个项目(~/vimfiles)中创建了一个文件类型文件project.vim

  4. project.vim文件中,我添加了以下自动命令行以启用文件类型检测:

    au BufNewFile,BufRead *project   setf project
    
  5. 然后,我在runtimepath的第一个项目(~/vimfiles)中的新目录ftplugin中创建了一个文件类型插件文件project.vim。在该文件中,我添加了以下setlocal选项用于注释:

    setlocal comments=:# commentstring=#\ %s formatoptions-=t formatoptions+=croql
    
  6. 由于ftdetectftplugin目录都未出现在set runtimepath?的输出中,因此我在我的vimrc中添加了以下行:

    set rtp+=~/vimfiles/ftdetect,~/vimfiles/ftplugin
    
  7. 在我执行了这些操作后,set runtimepath?的输出中包括了先前缺失的目录,作为列出的最后一项:

    runtimepath=
     ~/vimfiles,
     ~\vimfiles\pack\plugins\start\VOoM,
     ~\vimfiles\pack\plugins\start\vimroom,
     ~\vimfiles\pack\plugins\start\vim-shell-master,
     ~\vimfiles\pack\plugins\start\vim-obsession-master,
     ~\vimfiles\pack\plugins\start\vim-misc-master,
     ~\vimfiles\pack\plugins\start\vim-addon-mw-utils,
     ~\vimfiles\pack\plugins\start\utl,
     ~\vimfiles\pack\plugins\start\tlib_vim,
     ~\vimfiles\pack\plugins\start\taskpaper.vim-master,
     ~\vimfiles\pack\plugins\start\tabular,
     ~\vimfiles\pack\plugins\start\supertab,
     ~\vimfiles\pack\plugins\start\limelight,
     ~\vimfiles\pack\plugins\start\goyo,
     ~\vimfiles\pack\plugins\start\ctrlpcache,
     ~\vimfiles\pack\plugins\start\ctrlp,
     C:\Program Files (x86)\Vim/vimfiles,
     C:\Program Files (x86)\Vim\vim90,
     ~\vimfiles\pack\plugins\start\tabular\after,
     C:\Program Files (x86)\Vim/vimfiles/after,
     ~/vimfiles/after,
     ~/vimfiles/ftdetect,
     ~/vimfiles/ftplugin
    
  8. 然后,我创建了一个带有'project'文件类型扩展名的测试文件,并输入了一些文本,但未按照setlocal选项进行注释(即,以#开头的行应该以斜体显示)。

  9. 我在测试文件上运行了set ft?命令,返回filetype=project。当我将文件类型更改为set ft=conf时,注释按预期显示。 'conf' 的文件类型文件具有相同的setlocal选项用于注释。

  10. 我在~/vimfiles/ftplugin/project.vim中添加了一个statusline的setlocal选项。相应的statusline出现在测试文件中。 运行set ft?命令返回的仍然是filetype=project

  11. 现在我有些困惑,欢迎任何帮助。

英文:

I'm using Vim 9.0 on Windows 10.

I've created a new filetype called 'projects' but setting local options for comments is not working. Here is what I've done:

  1. I added the line :filetype plugin indent on to my vimrc file.

  2. The :filetype command returns: filetype detection:ON plugin:ON indent:ON.

  3. I then created a filetype file called project.vim in a new directory called ftdetect in the first item of runtimepath (~/vimfiles).

  4. In the project.vim file I added the autocommand line to enable filetype detection:

    au BufNewFile,BufRead *project   setf project
    
  5. I then created a filetype plugin file called project.vim in a new directory called ftplugin in the first item of runtimepath (~/vimfiles). In that file I added the following setlocal options for comments:

    setlocal comments=:# commentstring=#\ %s formatoptions-=t formatoptions+=croql
    
  6. As neither the directory ftdetect or ftplugin were appearing in the output of :set runtimepath?, I added this line to my vimrc:

    set rtp+=~/vimfiles/ftdetect,~/vimfiles/ftplugin
    
  7. After I did this the output of :set runtimepath? included those previously missing directories as the last listed items:

    runtimepath=
    ~/vimfiles,
    ~\vimfiles\pack\plugins\start\VOoM,
    ~\vimfiles\pack\plugins\start\vimroom,
    ~\vimfiles\pack\plugins\start\vim-shell-master,
    ~\vimfiles\pack\plugins\start\vim-obsession-master,
    ~\vimfiles\pack\plugins\start\vim-misc-master,
    ~\vimfiles\pack\plugins\start\vim-addon-mw-utils,
    ~\vimfiles\pack\plugins\start\utl,
    ~\vimfiles\pack\plugins\start\tlib_vim,
    ~\vimfiles\pack\plugins\start\taskpaper.vim-master,
    ~\vimfiles\pack\plugins\start\tabular,
    ~\vimfiles\pack\plugins\start\supertab,
    ~\vimfiles\pack\plugins\start\limelight,
    ~\vimfiles\pack\plugins\start\goyo,
    ~\vimfiles\pack\plugins\start\ctrlpcache,
    ~\vimfiles\pack\plugins\start\ctrlp,
    C:\Program Files (x86)\Vim/vimfiles,
    C:\Program Files (x86)\Vim\vim90,
    ~\vimfiles\pack\plugins\start\tabular\after,
    C:\Program Files (x86)\Vim/vimfiles/after,
    ~/vimfiles/after,
    ~/vimfiles/ftdetect,
    ~/vimfiles/ftplugin
    
  8. I then created a test file with the project filetype extension and entered some text, but it was not commented as per the setlocal option (ie: the text in lines prepended with # should be italicized).

  9. I ran the command :set ft? on the test file, which returned filetype=project. When I change the filetype to set ft=conf the comments appear as expected. The filetype file for 'conf' has identical setlocal options for comments.

  10. I added a setlocal option for a statusline to ~/vimfiles/ftplugin/project.vim. The corresponding statusline became present in the test file. Running the :set ft? command indicated returned filetype=project as before.

  11. I am a now a bit stuck and would welcome any help.

答案1

得分: 1

(6) 和 (7) 是不需要的和无用的。只有 "vimfiles" 必须在 runtimepath 中。"ftdetect" 和 "ftplugin" 总是相对于它来设置。

(8) 语法与您设置的选项无关。相反,您必须创建正则表达式并启用语法支持。如何做到这一点是一个独立的主题,因此建议您从 ":h syntax" 开始详细阅读以及类似的内容。

英文:

(6) and (7) are unneeded and useless. Only "vimfiles" must be in runtimepath. "ftdetect" and "ftplugin" are always taken relative to it.

(8) Syntax does not have anything to do with options you set. Instead, you have to create regexes and enable syntax support. How to do it is a topic of its own, so you're adviced to start long reading at :h syntax and all such.

huangapple
  • 本文由 发表于 2023年5月30日 06:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定