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

时间:2011-05-30 08:04:21

标签: vim plugins indentation

我正在开展几个项目,每个项目都使用不同的缩进样式(针对各种文件类型)。例如,每个缩进级别1个选项卡,2或4个空格等。如何在这些不同样式之间自动切换?我通常喜欢使用制表符缩进,但我厌倦了在使用空格缩进代码时必须输入:set expandtabs。可能的解决方案包括根据文件路径或项目根目录中的某些配置加载一段vim配置。是否有一个插件可以优雅地为我解决这个问题?

5 个答案:

答案 0 :(得分:15)

  1. 查看cinoptions选项和softtabstop选项(以及expandtab,但您知道这一点。)
  2. 在'〜/ .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
    

答案 1 :(得分:14)

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

  

有时候,当你处理不同的项目时,你会遇到问题,他们使用不同的缩进,标签扩展等等。每个项目都需要vimrc来覆盖〜/ .vimrc

中的首选设置

答案 2 :(得分:4)

EditorConfig及其Vim plugin

  

什么是EditorConfig?

     

EditorConfig可帮助开发人员定义和维护一致的编码   不同编辑器和IDE之间的样式。 EditorConfig项目   由用于定义编码样式和集合的文件格式组成   文本编辑器插件,使编辑器能够读取文件格式和   坚持定义的风格。 EditorConfig文件易于阅读和   它们可以很好地与版本控制系统配合使用。

在其他一些事情中它允许你设置缩进。它是非常简单且最重要的标准化方式,受到许多不同编辑器和IDE的支持,因此它不仅可以为您设置适当的缩进,还可以为所有从事项目工作的人设置适当的缩进。

您只需要在项目根目录中创建一个.editorconfig文件,Vim会自动找到它(假设您已安装了插件),并设置正确的值。示例配置文件:

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

答案 3 :(得分:3)

是的,有:如果您正在使用Project Plugin,则可以指定每次打开项目文件时评估其内容的文件(此文件为叫in.vim)。与in.vim相反的是out.vim:每次离开项目时都会执行此操作。

答案 4 :(得分:1)

对于Editorconfig支持的所有内容,正确的答案是使用它,请参阅单独的答案。

对于其他设置,您可以将<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="[your fragment location]" android:id="@+id/fragment_container" /> </LinearLayout> 文件与.vimrc一起使用。

请参见set exrc secure

: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