如何在 vim 中实现项目特定缩进?

发布于 2024-11-10 13:47:47 字数 202 浏览 4 评论 0原文

我正在开发几个项目,每个项目都使用不同的缩进样式(针对各种文件类型)。例如,每个缩进级别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回梦 2024-11-17 13:47:47
  1. 查看cinoptions 选项和softtabstop 选项(还有expandtab,但您知道这一点)。
  2. 在你的 '~/.vimrc' 中,为保存某些项目源代码的每个目录定义缓冲区条目自动命令,例如:

    augroup 项目设置
    au BufRead,BufEnter /path/to/project1/* set et sts=2 cindent cinoptions=...
    au BufRead,BufEnter /path/to/project2/* set noet sts=4 cindent cinoptions=...
    澳大利亚集团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
    
  1. Look at the cinoptions option and softtabstop option (and expandtab, but you know that).
  2. In your '~/.vimrc', define buffer entry auto-commands for each directory where you keep sources of some project like:

    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
    

    If the project has mixture of languages and needs different settings for then, you can also
    add extensions like:

    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
    
恏ㄋ傷疤忘ㄋ疼 2024-11-17 13:47:47

我使用插件 localvimrc ,它完全符合您的要求:

有时,当您处理不同的项目时,您会遇到问题,它们使用不同的缩进、制表符扩展等。每个项目都需要 vimrc 来覆盖 ~/.vimrc 中的首选设置

I use the plugin localvimrc, which does exactly what you are asking for:

Sometimes, when you work on different projects, you have the problem, that they use different indentation, tab expansion and so on. You need vimrc for each project that overrides your prefered settings from ~/.vimrc

奢华的一滴泪 2024-11-17 13:47:47

EditorConfig ,它是 Vim 插件

什么是 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

EditorConfig and it's Vim plugin:

What is EditorConfig?

EditorConfig helps developers define and maintain consistent coding
styles between different editors and IDEs. The EditorConfig project
consists of a file format for defining coding styles and a collection
of text editor plugins that enable editors to read the file format and
adhere to defined styles. EditorConfig files are easily readable and
they work nicely with version control systems.

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:

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
江湖正好 2024-11-17 13:47:47

是的,有:如果您使用 项目插件,您可以指定一个文件,每次打开项目的文件时都会评估其内容(该文件称为 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 of in.vim is out.vim: this one's executed every time you leave the project.

风蛊 2024-11-17 13:47:47

对于 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.

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 with set exrc secure.

See :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)

See :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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文