如何在 vim 中实现项目特定缩进?
我正在开发几个项目,每个项目都使用不同的缩进样式(针对各种文件类型)。例如,每个缩进级别 1 个制表符、2 个或 4 个空格等。如何在这些不同样式之间自动切换?我通常更喜欢使用制表符缩进,但我厌倦了在处理空格缩进代码时必须始终键入 :set Expandtabs。可能的解决方案包括根据文件路径或项目根目录中的某些配置加载一段 vim 配置。有没有一个插件可以以优雅的方式为我解决这个问题?
I'm working on several projects, each of which uses different indentation style (for various filetypes). For example 1 tab per indentation level, 2 or 4 spaces etc. How can I automate switching between these different styles? I normaly prefer indenting with tabs, but I'm tired of having to type :set expandtabs all the time when working with space-indented code. Possible solutions would include loading a piece of vim configuration based on file path or some configuration in the project root. Is there a plugin to solve this for me in an elegant way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
cinoptions
选项和softtabstop
选项(还有expandtab
,但您知道这一点)。在你的 '~/.vimrc' 中,为保存某些项目源代码的每个目录定义缓冲区条目自动命令,例如:
如果项目混合了多种语言并且需要不同的设置,您也可以
添加扩展,例如:
cinoptions
option andsofttabstop
option (andexpandtab
, but you know that).In your '~/.vimrc', define buffer entry auto-commands for each directory where you keep sources of some project like:
If the project has mixture of languages and needs different settings for then, you can also
add extensions like:
我使用插件 localvimrc ,它完全符合您的要求:
I use the plugin localvimrc, which does exactly what you are asking for:
EditorConfig ,它是 Vim 插件:
除此之外,它还允许您设置缩进,该缩进仅适用于该项目。这是一种非常简单且最重要的标准化方式,受到许多不同的编辑器和 IDE 支持,因此它不仅会为您而且可能为所有从事项目的人设置正确的缩进。
您只需在项目根目录中创建一个
.editorconfig
文件,Vim 会自动找到它(假设您安装了插件),并设置正确的值。 为每个项目创建.editorconfig
文件可以实现您所需要的 - 项目特定的缩进。示例配置文件:
EditorConfig and it's Vim plugin:
Among few other things it allows you to set indentation, which will be applied only for this project. It's very simple and most importantly standardized way supported by many different editors and IDE-s, so that it will set the proper indentation not only for you but likely all people working on a project.
You just need to create a
.editorconfig
file in the project root and Vim automatically finds it (assuming you have the plugin installed), setting the proper values. Creating.editorconfig
files for each project achieves what you need - project specific indentation.Example config file:
是的,有:如果您使用 项目插件,您可以指定一个文件,每次打开项目的文件时都会评估其内容(该文件称为
in.vim
)。in.vim
的反面是out.vim
:每次离开项目时都会执行这个。Yes, there is: If you're using the Project Plugin, you could specify a file whose content is being evaluated every time you open a file of the project (this file is called
in.vim
). The opposite ofin.vim
isout.vim
: this one's executed every time you leave the project.对于 Editorconfig 支持的所有内容,正确的答案是使用它,请参阅单独的答案。
不过,对于其他设置,您可以使用带有
set exrc secure
的.vimrc
文件。请参阅
:h exrc
:请参阅
:h secure
:For everything that Editorconfig supports, the right answer is to use it, see the separate answer for that.
For other settings though, you could use
.vimrc
files withset exrc secure
.See
:h exrc
:See
:h secure
: