Andy Stewart
Andy Stewart

Reputation: 5498

How to restart vim from a bash script?

I want to restart vim from a bash script so that vim picks up out-of-band changes. I almost have it working but I am stuck trying to determine what to use to launch vim.

Here's what I have:

#!/usr/bin/env bash
local servername=$(vim --serverlist)
[ -n "$servername" ] && {
  vim --servername "$servername" --remote-send '<C-\><C-N>:mks! _session.vim<CR>:wqa<CR>'
  vim -S _session.vim
  sleep 1
  rm _session.vim
}

The problem is the vim called by the script is the very obsolete system vim at /usr/bin/vim, not "my" vim which is an alias to mvim -v (where mvim is the launch script which comes with MacVim).

This has two unfortunate consequences: (1) the system vim doesn't recognise --serverlist; (2) even if it did my script would subsequently launch the wrong vim.

What's the best way to invoke the vim on my path?

Upvotes: 1

Views: 2545

Answers (1)

romainl
romainl

Reputation: 196751

  1. The default vim is never built with +clientserver so the portability you are afraid to loose was never there to begin with.

  2. Aliases are not expanded in bash scripts so your script won't see mvim -v if you don't tell it explicitly to use that. Furthermore, your vim is an alias so it is not in your PATH.

  3. You could define an environment variable somewhere near the top of your script and use it instead of vim:

    #!/usr/bin/env bash
    
    VIM='/path/to/mvim'
    
    "$VIM" -v whatever
    

    Or turn your alias into a proper script.

    Or, maybe, place mvim earlier in your PATH and call mvim -v explicitly.

Upvotes: 2

Related Questions