How can I make Vim open files in `--remote-silent` without getting an error when running just `vim`?

Viewed 648

To open files in a pre-existing instance of Vim (MacVim in my case), I took Derek Wyatt's advice and added this to my bash_profile:

alias mvim='mvim --remote-silent'

This works as long as I pass mvim an argument (mvim myFile, mvim ., etc.), but if I run mvim by itself, I get an error: Argument missing after: "--remote-silent"

So I replaced the above alias with the following function:

function mvim() {
  if [ $# > 0 ] ; then
    command mvim --remote-silent "$@"
  else
    command mvim
  fi
}

Now if I run mvim without an argument, I get the same error and a file named 0 gets written to the current directory. Things are still fine if I pass mvim arguments.

What am I missing here, and what's the best way to handle this?

Thanks Ingo Karkat for clarifying. If anyone's interested, here's how I'm handling this now:

function ivim {
  if [ -n "$1" ] ; then
    command mvim --remote-silent "$@"
  elif [ -n "$( mvim --serverlist )" ] ; then
    command mvim --remote-send ":call foreground()<CR>:enew<CR>:<BS>"
  else
    command mvim
  fi
}

The :<BS> at the end of the elif branch is just to clear the command line. It feels a little hacky, but I don't know how else to achieve this.

2 Answers
Related