cinoptions
选项和softtabstop
选项(还有expandtab
,但您已经知道了)。在您的'~/.vimrc'中,为每个存储某个项目源代码的目录定义缓冲区入口自动命令,例如:
augroup ProjectSetup
au BufRead,BufEnter /path/to/project1/* set et sts=2 cindent cinoptions=...
au BufRead,BufEnter /path/to/project2/* set noet sts=4 cindent cinoptions=...
augroup END
如果项目中有多种语言并且需要不同的设置,您也可以添加扩展程序,例如:
au BufRead,BufEnter /path/to/project1/*.{c,h} set noet sts=0 cindent cinoptions=...
au BufRead,BufEnter /path/to/project1/*.py set et sts=4
我使用插件localvimrc,它能够准确地实现您所要求的功能:
有时候,当你在不同的项目中工作时,会遇到这样的问题,它们使用不同的缩进、制表符扩展等。你需要为每个项目编写一个vimrc,该文件将覆盖你在~/.vimrc中设置的首选项。
什么是EditorConfig?
EditorConfig帮助开发人员在不同的编辑器和IDE之间定义和维护一致的编码风格。EditorConfig项目由一个文件格式和一组文本编辑器插件组成,这些插件使编辑器能够读取文件格式并遵守定义的风格。EditorConfig文件易于阅读,并且它们与版本控制系统很好地配合。
除其他功能外,它允许您设置缩进,该缩进仅适用于此项目。这是一种非常简单且最重要的标准化方法,被许多不同的编辑器和IDE支持,因此它不仅会为您设置正确的缩进,而且可能会为所有参与项目的人设置。
您只需要在项目根目录中创建一个.editorconfig
文件,Vim会自动找到它(假设您已安装插件),并设置正确的值。 为每个项目创建.editorconfig
文件可以实现您所需的 - 项目特定的缩进。
示例配置文件:
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.{js,html}]
indent_size = 2
[*.css]
indent_size = 4
是的,有这个功能:如果您正在使用Project Plugin,您可以指定一个文件,在每次打开项目文件时都会评估该文件的内容(此文件称为in.vim
)。与in.vim
相反的是out.vim
:每次退出项目时都会执行此文件。
:make
或:grep
之后)或显式命令(在C / C ++中:e%:r.h
通常非常有用)而不是从项目插件进入缓冲区,则不会运行它。 - Jan Hudec对于 Editorconfig 支持的所有内容,正确的答案是要使用它,详见单独的回答。
对于其他设置,你可以使用带有 set exrc secure
的 .vimrc
文件。
参见 :h exrc
:
d. If the 'exrc' option is on (which is NOT the default), the current
directory is searched for three files. The first that exists is used,
the others are ignored.
- The file ".vimrc" (for Unix, Amiga and OS/2) (*)
"_vimrc" (for MS-DOS and Win32) (*)
- The file "_vimrc" (for Unix, Amiga and OS/2) (*)
".vimrc" (for MS-DOS and Win32) (*)
- The file ".exrc" (for Unix, Amiga and OS/2)
"_exrc" (for MS-DOS and Win32)
:h secure
: When on, ":autocmd", shell and write commands are not allowed in
".vimrc" and ".exrc" in the current directory and map commands are
displayed. Switch it off only if you know that you will not run into
problems, or when the 'exrc' option is off. On Unix this option is
only used if the ".vimrc" or ".exrc" is not owned by you. This can be
dangerous if the systems allows users to do a "chown". You better set
'secure' at the end of your ~/.vimrc then.
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.
FileType
事件。但它的模式匹配文件类型,所以你必须自己检查路径。或者你可以将代码放在~/.vim/after/ftplugin/<language>.vim
中,但你仍然需要检查路径。 - Jan Hudec