如何使用命令或本地 vimrc 文件在多个 vim 配置之间切换? [英] How to switch between multiple vim configurations with a command or local vimrc files?

查看:28
本文介绍了如何使用命令或本地 vimrc 文件在多个 vim 配置之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个小组工作,每个小组都有自己的 C 语言制表符/缩进/间距标准.

I work in several groups, each of which has its own tab/indentation/spacing standards in C.

当我编辑文件时,有没有办法为每个文件设置单独的可选 VIM 配置:

Is there a way to have separate selectable VIM configurations for each so, when I edit a file, either:

  • 我执行类似 set group=1 的操作来选择配置
  • 位于工作目录中的本地 .vimrc 用于自动设置配置

推荐答案

总结

有几种方法可以做到这一点,其中大多数已被建议,但我想我会用两个额外的方法来总结它们:

In Summary

There are a few ways to do this, of which most have been suggested, but I thought I'd summarise them with two extra ones:

  • 每目录 vimrc - 缺点是 Vim 必须在正确的目录中启动:如果你的项目在 ~/project1 并且你有 ~/project1/.vim.custom 并执行 cd ~ ;vim project1/file.c,将找不到自定义设置.
  • 模式行 - 非常有效,但缺点是需要将它们添加到所有文件中(并记住将它们添加到新文件中)
  • 特定于目录的自动命令 - 这非常有效
  • 扫描文件中的特定标题(见下文) - 这是我过去在为不同公司或明确命名的项目工作时使用最多的方法
  • 打开文件时检查的每个目录的 vimrc(见下文).另一个相当容易实现的方法,尤其是当您的项目代码都在一个地方时.
  • Per-directory vimrc - has the disadvantage that Vim must be started in the right directory: if your project is in ~/project1 and you have ~/project1/.vim.custom and do cd ~ ; vim project1/file.c, the custom settings won't be found.
  • Modelines - very effective, but has the disadvantage of needing to add them to all files (and remember to add them to new files)
  • Directory specific autocommands - this is very effective
  • Scan for a specific header in the file (see below) - this is the one I've used most in the past where working for different companies or on clearly named projects
  • Per-directory vimrc that's checked when the file is opened (see below). Another fairly easy one to implement, especially if your project code is all in one place.

在许多组织中,每个源文件的顶部都有一个标准标题(带有版权声明和项目名称等).如果是这种情况,您可以让 Vim 自动扫描文件的前(例如)10 行以查找关键字.如果找到它,它可以更改您的设置.我已经修改了它以使其比我使用的表单更简单(它可以做很多其他事情),但是创建一个 ~/.vim/after/filetype.vim(如果你没有一个)并添加如下内容:

In a lot of organisations, there's a standard header (with a copyright notice and project name etc) at the top of every source file. If this is the case, you can get Vim to automatically scan the first (e.g.) 10 lines of the file looking for a keyword. If it finds it, it can change your settings. I've modified this to make it simpler than the form I use (which does lots of other things), but create a ~/.vim/after/filetype.vim (if you don't have one yet) and add something like this:

au FileType * call <SID>ConfigureFiletypes(expand("<amatch>"))

" List of file types to customise
let s:GROUPNAMETypes = ['c', 'cpp', 'vhdl', 'c.doxygen']

func! <SID>CheckForGROUPNAMECode()
    " Check if any of the first ten lines contain "GROUPNAME".

    " Read the first ten lines into a variable
    let header = getline(1)
    for i in range(2, 10)
        let header = header . getline(i)
    endfor

    if header =~ '\<GROUPNAME\>'
        " Change the status line to make it clear which
        " group we're using
        setlocal statusline=%<%f\ (GROUPNAME)\ %h%m%r%=%-14.(%l,%c%V%)\ %P
        " Do other customisation here
        setlocal et
        " etc
    endif
endfunc

func! <SID>ConfigureFiletypes(filetype)
    if index(s:GROUPNAMETypes, a:filetype) != -1
        call <SID>CheckForGROUPNAMECode()
    endif
endfunc

无论何时打开任何类型的文件并设置文件类型(au FileType * 行),都会调用 ConfigureFiletypes 函数.这将检查文件类型是否在与当前组 (GROUPNAME) 关联的文件类型列表中,在本例中为c"、cpp"、vhdl"或c​​.doxygen".如果是,它调用 CheckForGROUPNAMECode(),它读取文件的前 10 行,如果它们包含 GROUPNAME,它会进行一些自定义.除了设置扩展选项卡或其他任何内容外,这还会更改状态栏以清楚地显示组名称,以便您一目了然地知道它的工作原理.

Whenever a file of any type is opened and the file type is set (the au FileType * line), the ConfigureFiletypes function is called. This checks whether the file type is in the list of file types associated with the current group (GROUPNAME), in this case 'c', 'cpp', 'vhdl' or 'c.doxygen'. If it is, it calls CheckForGROUPNAMECode(), which reads the first 10 lines of the file and if they contain GROUPNAME, it does some customisation. As well as setting expandtabs or whatever, this also changes the status bar to show the group name clearly so you know it's worked at a glance.

很像 JS Bangs 的建议,拥有自定义配置文件会很有用.然而,与其在 vimrc 中加载它,不如考虑这样的事情,它将检查 .c 文件何时在与 .c 文件相同的目录中为 .vim.custom 打开.

Much like JS Bangs' suggestion, having a custom configuration file can be useful. However, instead of loading it in vimrc, consider something like this, which will check when a .c file is opened for a .vim.custom in the same directory as the .c file.

au BufNewFile,BufRead *.c call CheckForCustomConfiguration()

function! CheckForCustomConfiguration()
    " Check for .vim.custom in the directory containing the newly opened file
    let custom_config_file = expand('%:p:h') . '/.vim.custom'
    if filereadable(custom_config_file)
        exe 'source' custom_config_file
    endif
endfunction

这篇关于如何使用命令或本地 vimrc 文件在多个 vim 配置之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆