How to switch between multiple vim configurations with a command or local vimrc files?

后端 未结 12 1227
花落未央
花落未央 2020-12-04 05:43

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

Is there a way to have separate selectable VIM configurations for each so

相关标签:
12条回答
  • 2020-12-04 06:22

    After trying out the localvimrc plugin suggested by the previous poster, I very much like having non-futzy per-project control over vim settings.

    It does ask confirmation before loading a .lvimrc file by default but there is a setting to automatically load .lvimrc files. Some might see this as a security hole, but it works as advertised.

    I chose to .gitignore the .lvimrc files. Alternatively you can check them in as a form of shared settings (tab/space expansion, tabstops, other project-specific settings).

    0 讨论(0)
  • 2020-12-04 06:24

    You can also put autocommands in your .vimrc which set specific options on a per-path basis.

    au BufRead,BufNewFile /path/to/project1/* setl sw=4 et
    au BufRead,BufNewFile /path/to/project2/* setl sw=3 noet
    
    0 讨论(0)
  • 2020-12-04 06:29

    Plugin doing the right thing: http://www.vim.org/scripts/script.php?script_id=441

    “This plugin searches for local vimrc files in the filesystem tree of the currently opened file. By default it searches for all ".lvimrc" files from the file's directory up to the root directory and loads them in reverse order. The filename and amount of loaded files is customizable through global variables.”

    0 讨论(0)
  • 2020-12-04 06:29

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

    I work with all sorts of open source, all at the same time. It's not practical to be creating separate .vimrc files and reconfiguring the formatting standards. More than a decade ago, I finally got tired of dealing with the editor configuration and wrote a program called autotab to handle it.

    When autotab is set up with Vim suggested, each time you load a file into Vim, autotab is invoked on it, and the Vim settings output autotab are passed to a :set command.

    autotab reads several thousand lines from the file, analyzes them and determines the settings for the expandtab, tabstop and shiftwidth parameters.

    It figures out whether the file uses hard tabs or just spaces for indentation, and it figures out the indentation size. If the file is indented with tabs, it figures out the right tab size, based on rendering the file sample using various tab sizes and judging it according to heuristics like line-over-line alignment of internal elements.

    It works well enough that I stopped tweaking the algorithm years ago. If it gets confused, it's almost always because the file has formatting issues, such as the use of multiple conventions at the same time.

    It is also "agnostic" of the file type and works well with a variety of different languages. I use it not only over C, but shell scripts, Lisp, Makefiles, HTML, and what have you.

    Note that it doesn't handle other parameters of formatting that may be project-specific, like for instance, in C files, whether case labels in a switch statement are indented or not, or whether wrapped function argument lists are simply indented, or aligned to the opening parenthesis of the argument list. Vim does have settings for that sort of thing, and so the program could be plausibly extended to analyze the style and output those parameters.

    0 讨论(0)
  • 2020-12-04 06:29

    Looking for mostly the same issue I also found the Sauce plug-in: http://www.vim.org/scripts/script.php?script_id=3992

    It claims:

    Sauce is a lightweight manager for multiple vimrc files, which can be used to load different settings for different environments. In short, you can maintain lots of different vim settings files and only load the one(s) you need when you need them.

    I find it particularly interesting that it keeps it configuration all in its data directory instead of expecting the user to sprinkle dotfiles across the filesystem. This though often rather a metter of personal taste.

    I have yet to test it though.

    0 讨论(0)
  • 2020-12-04 06:29

    You can use stow for switching configuration (any dotfiles, not only .vimrc)

    Install stow:

    $ apt install stow
    

    Create multiple directories for each configurations:

    ~$ ls -d ~/dotfiles/vim*
    vim-all vim-webdev vim-go
    

    Put different .vimrc's in them:

    $ find ~/dotfiles -name .vimrc
    /home/username/vim-golang/.vimrc
    /home/username/vim-webdev/.vimrc
    /home/username/vim-all/.vimrc
    

    Now you can instantinate vim-golang config with this command (should be run inside dotfiles directory):

    ~$ cd ~/dotfiles
    
    dotfiles$ stow -v vim-golang
    LINK: .vimrc => dotfiles/vim-golang/.vimrc
    

    Now it's linked:

    $ cd ~ && ls -l .vimrc 
    .vimrc -> dotfiles/vim-golang/.vimrc
    

    If you need to switch config, just re-stow it:

    ~$ cd dotfiles
    
    dotfiles$ stow -v -D vim-golang
    UNLINK: .vimrc
    
    dotfiles$ stow -v vim-webdev
    LINK: .vimrc => dotfiles/vim-webdev/.vimrc
    
    $ cd ~ && ls -l .vimrc 
    .vimrc -> dotfiles/vim-webdev/.vimrc
    

    More reading of it here: Managing dotfiles with GNU stow

    Pros: pretty simple, no vim plugin dependencies, can be used for managing all dotfiles, not only .vimrc.

    Cons: configs are independent of each other, you need to manage/update each of them separately (if you dont switch/update you configs too often - it'll not be the issue).

    0 讨论(0)
提交回复
热议问题