3

Repost from stackoverflow after suggestion

I am trying to move from vim to neovim. I have a strange problem.

I have, in my .bashrc, exported $VIMINIT to .config/vim/vimrc so that vim files like .viminfo are not polluting my home folder. This trick was suggested in stackoverflow and if I remember correctly, it was only thing that actually removed all .vim* files out of my home directory.

But neovim also seems to give priority to $VIMINIT over its own config folder. I cloned lunarvim as .config/nvim and upon launching neovim, it wasn't using it. Only after I commented out the export statement from my bashrc file, it detected its config folder.

I don't want to remove my well tested vim setup without getting comfortable with neovim first. What is a safe and appropriate way to separate neovim and vim? I want to have .config/vim and .config/nvim as config location for settings and generated junks respectively and not have any related dotfiles in home folder.

Update: Based on @martin-tournoij suggestion, currently this is setting in my viminit proxy file

let g:nvim_path='~/dotfiles/editors/nvim'
let g:vim_path='~/.config/vim'

if has('nvim')
    " let g:runtimepath += '~/.config/nvim/'
    execute 'set runtimepath+=' . g:nvim_path
    execute 'set packpath+=' . g:nvim_path . '/plugins'
    execute 'source '. g:nvim_path . '/init.lua'
    finish
else 
    execute 'source '. g:vim_path . '/vimrc'
    finish
endif

Any suggestion to improve this?

2
  • 3
    The Stack Overflow post you link to doesn't mention VIMINIT anywhere. That said, VIMINIT is "used as an Ex command line", so you could just check for Neovim in it and source a different file. Or you could write a wrapper for neovim: nvim () ( unset VIMINIT; nvim "$@"; ) or alias nvim='env -u VIMINIT nvim'.
    – muru
    Commented Jun 7, 2022 at 7:00
  • I wouldn't recommend a "distribution" like lunarvim—you will have a far better time actually learning and customizing the editor to behave the way you want. Note that the first initialization found is used, so VIMINIT blocks, say, vimrc files (:help VIMINIT). A better solution might be to use -i or set 'viminfofile' if that was your only concern. (There's also 'undodir', 'directory', 'backupdir', etc.)
    – D. Ben Knoble
    Commented Jun 7, 2022 at 15:21

1 Answer 1

2

Both Vim and Neovim will use the same VIMINIT environment variable to set the configuration directory, so setting a global one will apply to both.

You have two options:

  1. Set it only for vim commands, or unset it for nvim commands:

    alias vim='VIMINIT=.config/vim/vimrc vim'
    # or:
    alias nvim='env -u VIMINIT nvim'
    
  2. Source a different file in your vimrc if neovim loads it:

    if has('nvim')
        source ~/.config/nvim/vimrc
        finish
    endif
    

    To get a full separation you also need to tweak some other paths here as well, such as runtimepath and packpath. It's actually a bit complex how this all works.

I recommend the first option as it's a lot easier and keeps a clean separation between the two.

5
  • Follow-up on your second technique, Suppose, I set VIMINIT as ~/.config/vim/viminit and within that file, I simply write conditional sourcing, if has('vim') source .config/vim/vimrc else if has ('nvim') source .config/nvim/init.vim Will this work, keeping all my configuration file and folders at their respective place and sourcing based on condition. Commented Jun 10, 2022 at 4:22
  • 1
    There is no has('vim') @AbhinavKulshreshtha and it will always be "false", but you can use if has('nvim') [..nvim..] else [..vim..] endif. It should work, but things like custom syntax files, after/ directory, and plugins will be shared between Vim and Neovim, which may be what you want, or it may not be. Commented Jun 10, 2022 at 15:42
  • The if-else technique works. I pointed VIMINIT to a vim_proxy file which contains the if else condition pointing to respective rc files. Now I have both lunarvim and my old vim setup working as expected. Can you elaborate your point about runtimepath and packpath for full complex separation? Do you know a repo which I can look at for reference? Commented Jun 14, 2022 at 13:12
  • can you please check the vim_proxy file I updated on the question and give me suggestion to improve full separation? Commented Jun 20, 2022 at 13:58
  • @AbhinavKulshreshtha; the way vim works with all these paths is a bit complex and I don't remember things off-hand, so I don't really know. If you want "full separation" then not setting VIMINIT for both is by far the easiest and most fool-proof. Commented Jun 20, 2022 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.