英文:
Vim: Filetype plugin setlocal options for comments and commentstring not appearing in buffer
问题
我正在使用Windows 10上的Vim 9.0。
我已经创建了一个新的文件类型称为'projects',但是设置本地注释选项不起作用。以下是我所做的:
-
我在我的vimrc文件中添加了以下行:
:filetype plugin indent on
。 -
:filetype
命令返回:filetype detection:ON plugin:ON indent:ON
。 -
然后,我在一个新的目录
ftdetect
中的runtimepath
的第一个项目(~/vimfiles
)中创建了一个文件类型文件project.vim
。 -
在
project.vim
文件中,我添加了以下自动命令行以启用文件类型检测:au BufNewFile,BufRead *project setf project
-
然后,我在
runtimepath
的第一个项目(~/vimfiles
)中的新目录ftplugin
中创建了一个文件类型插件文件project.vim
。在该文件中,我添加了以下setlocal选项用于注释:setlocal comments=:# commentstring=#\ %s formatoptions-=t formatoptions+=croql
-
由于
ftdetect
或ftplugin
目录都未出现在set runtimepath?
的输出中,因此我在我的vimrc中添加了以下行:set rtp+=~/vimfiles/ftdetect,~/vimfiles/ftplugin
-
在我执行了这些操作后,
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
-
然后,我创建了一个带有'project'文件类型扩展名的测试文件,并输入了一些文本,但未按照setlocal选项进行注释(即,以
#
开头的行应该以斜体显示)。 -
我在测试文件上运行了
set ft?
命令,返回filetype=project
。当我将文件类型更改为set ft=conf
时,注释按预期显示。 'conf' 的文件类型文件具有相同的setlocal
选项用于注释。 -
我在
~/vimfiles/ftplugin/project.vim
中添加了一个statusline的setlocal
选项。相应的statusline出现在测试文件中。 运行set ft?
命令返回的仍然是filetype=project
。 -
现在我有些困惑,欢迎任何帮助。
英文:
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:
-
I added the line
:filetype plugin indent on
to my vimrc file. -
The
:filetype
command returns:filetype detection:ON plugin:ON indent:ON
. -
I then created a filetype file called
project.vim
in a new directory calledftdetect
in the first item ofruntimepath
(~/vimfiles
). -
In the
project.vim
file I added the autocommand line to enable filetype detection:au BufNewFile,BufRead *project setf project
-
I then created a filetype plugin file called
project.vim
in a new directory calledftplugin
in the first item ofruntimepath
(~/vimfiles). In that file I added the following setlocal options for comments:setlocal comments=:# commentstring=#\ %s formatoptions-=t formatoptions+=croql
-
As neither the directory
ftdetect
orftplugin
were appearing in the output of:set runtimepath?
, I added this line to my vimrc:set rtp+=~/vimfiles/ftdetect,~/vimfiles/ftplugin
-
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
-
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). -
I ran the command
:set ft?
on the test file, which returnedfiletype=project
. When I change the filetype toset ft=conf
the comments appear as expected. The filetype file for 'conf' has identicalsetlocal
options for comments. -
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 returnedfiletype=project
as before. -
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论