当我在.{cpp,h}
文件中的单行评论结尾处开始新行时,vim
会自动对其进行评论。例如:
// This is a comment<CR>
// | <- Cursor is moved to `|`, `//` is automatically inserted.
我不确定这是插件还是设置。我看不到任何看起来像是在~/.vimrc
中执行此操作的内容,下面列出了加载的插件。
我喜欢这个/* */
- 样式多行评论,但我不希望我的单行评论正在运行默认情况下多行。
哪个设置(或插件)可以执行此操作,我可以将其关闭仅针对此评论类型吗?
:scriptnames
给出了这个:
1: /Users/simont/.vimrc
2: /usr/local/share/vim/vim73/syntax/syntax.vim
3: /usr/local/share/vim/vim73/syntax/synload.vim
4: /usr/local/share/vim/vim73/syntax/syncolor.vim
5: /usr/local/share/vim/vim73/filetype.vim
6: /usr/local/share/vim/vim73/ftplugin.vim
7: /usr/local/share/vim/vim73/syntax/nosyntax.vim
8: /Users/simont/repositories/config-files/vim/colors/solarized.vim
9: /usr/local/share/vim/vim73/plugin/getscriptPlugin.vim
10: /usr/local/share/vim/vim73/plugin/gzip.vim
11: /usr/local/share/vim/vim73/plugin/matchparen.vim
12: /usr/local/share/vim/vim73/plugin/netrwPlugin.vim
13: /usr/local/share/vim/vim73/plugin/rrhelper.vim
14: /usr/local/share/vim/vim73/plugin/spellfile.vim
15: /usr/local/share/vim/vim73/plugin/tarPlugin.vim
16: /usr/local/share/vim/vim73/plugin/tohtml.vim
17: /usr/local/share/vim/vim73/plugin/vimballPlugin.vim
18: /usr/local/share/vim/vim73/plugin/zipPlugin.vim
19: /usr/local/share/vim/vim73/scripts.vim
20: /usr/local/share/vim/vim73/ftplugin/vim.vim
21: /usr/local/share/vim/vim73/syntax/vim.vim
答案 0 :(得分:14)
au FileType c,cpp setlocal comments-=:// comments+=f://
在你的vimrc中,应该在{cpp,h}文件中为//
执行操作而不影响块注释。
要在当前缓冲区暂时尝试使用:
:setlocal comments-=:// comments+=f://
答案 1 :(得分:6)
这种与特定文件类型相关的配置通常通过文件类型插件设置。 Vim附带了许多常见文件类型的文件类型(例如.cpp
)。您可以使用:set ft?
检查缓冲区的文件类型。
pb2q说,开始换行后继续注释的设置来自选项'comments'
。
对于.{cpp,h}
,默认文件类型为“cpp”,'comment'
选项设置为$VIMRUNTIME/ftplugin/c.vim
,因为cpp.vim
位于同一目录中。来自c.vim
文件:
" Set 'comments' to format dashed lists in comments.
setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://
comments
选项是{flags}:{string}
的列表,标记f
和O
避免扩展评论新行。
来自Vim FAQ:
You can use an autocommand triggered on the FileType event:
au Filetype * set formatoptions=xyz
This should at least be after "filetype on" in your vimrc. Best is to put
it in your "myfiletypefile" file, so that it's always last.
If you want to override a setting for a particular filetype, then create a
file with the same name as the original filetype plugin in the
~/.vim/after/ftplugin directory For example, to override a setting in the
c.vim filetype plugin, create a c.vim file in the ~/.vim/after/ftplugin
directory and add your preferences in this file.
所以用
创建文件~/.vim/after/ftplugin/c.vim
setlocal comments-=://
setlocal comments+=fO://
应该解决问题。
答案 2 :(得分:0)
您可以使用在FileType事件上触发的自动命令:
au Filetype * set formatoptions=xyz
这至少应该在vimrc中的“ filetype on”之后。最好是放 它放在您的“ myfiletypefile”文件中,因此它总是最后一个。
如果要覆盖特定文件类型的设置,请创建一个 与原始文件类型插件中的名称相同的文件 〜/ .vim / after / ftplugin目录例如,要覆盖 c.vim文件类型插件,在〜/ .vim / after / ftplugin中创建一个c.vim文件 目录并在此文件中添加您的首选项。 因此,使用
创建文件〜/ .vim / after / ftplugin / c.vimsetlocal注释-=:// setlocal评论+ = fO://