我的vim用于在php中自动继续评论.例如:
/* | <- cursor here
Run Code Online (Sandbox Code Playgroud)
然后,按下Enter给我:
/*
* | <- cursor here
Run Code Online (Sandbox Code Playgroud)
再次,给了我:
/*
*
* | <- cursor here
Run Code Online (Sandbox Code Playgroud)
等等...
据我了解,这是由comments和formatoptions选项控制的.但是,每当我打开一个php文件时,comments设置为:
s:<!--,m: ,e:-->
我查看了〜/ .vim文件夹,以及$ VIMRUNTIME文件夹,我无法找到更改的位置/原因,以及comments选项设置错误的原因.
这是我的.vimrc的链接
请注意,如果在.vimrc 中具有文件类型 indent on和plugin on单独的行,也会出现此问题:
filetype indent on
filetype plugin on
Run Code Online (Sandbox Code Playgroud)
这导致$VIMRUNTIME/indent/php.vim被处理之前$VIMRUNTIME/ftplugin/php.vim。
该indent/php.vim文件会重置'comments',但ftplugin/php.vim没有。
indent/php.vim获取来源并comments正确设置:
setlocal comments=s1:/*,mb:*,ex:*/,://,:#
Run Code Online (Sandbox Code Playgroud)
然后ftplugin/php.vim找货源。它再次来自ftplugin/html.vim:
runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
Run Code Online (Sandbox Code Playgroud)
这导致ftplugin/html.vim被处理和设置:
setlocal commentstring=<!--%s-->
setlocal comments=s:<!--,m:\ \ \ \ ,e:-->
Run Code Online (Sandbox Code Playgroud)
后来在ftplugin/php.vim在commentstring是复位而不是comments:
setlocal commentstring=/*%s*/
Run Code Online (Sandbox Code Playgroud)
filetype indent on
filetype plugin on
Run Code Online (Sandbox Code Playgroud)
在任何情况下,插件都应该在缩进之前处理。
要检查包含/处理的顺序,请查看:scriptnames.
使用版本 7.3(补丁集 754)的默认设置,我观察到与原始帖子中相同的错误:
/**<ENTER>
Run Code Online (Sandbox Code Playgroud)
预期结果:
/**
* <cursor>
Run Code Online (Sandbox Code Playgroud)
实际结果:
/**
<cursor>
Run Code Online (Sandbox Code Playgroud)
该解决方案包括两个步骤:
对我的 vimrc 的修改考虑了这两个步骤:
au FileType php setlocal comments=s1:/*,mb:*,ex:*/,://,:#
au FileType php setlocal formatoptions+=cro
Run Code Online (Sandbox Code Playgroud)
欢呼!