1

When using vim inside my Bash-shell, I will sometimes use Ctrl-Z to leave vim and do other things in the shell. Later, I will then use the fg command to return to vim. However, it sometimes happens that instead of returning to vim with fg, I start a new instance of vim when I find some file I want to edit. That means I have two instances of vim running in the same shell, and I don't like this idea. I want to force myself to only run one instance per shell.

Is there some way I can block vim from starting if one instance is already running in the current shell? Can this be implemented in the .vimrc instead of making a shell script that runs when I type vim?

  • Are you certain this wouldn’t interfere with your workflow? Very often I suspend Vim precisely to open another instance on a different file someplace else. Perhaps it’d be better to have $PS1 indicate that the current shell already has a backgrounded Vim. – phg Jan 5 '17 at 17:14
  • @phg Well the severe case is when you attempt to open a file that was already opened by an old instance. Since I don't use the swapfile thing, I am not notified of that. Having the same file open in two instances is bad IMO. – vatsug Jan 5 '17 at 17:24
3

This in your .bashrc might work, but may hit some false positives:

alias vim='if jobs | grep -q vim; then fg; else command vim; fi'
  • 3
    You could drop the absolute path and use command vim instead. And $@ needs to be double quoted. – Kusalananda Jan 5 '17 at 17:21
  • Noted and incorporated. – DopeGhoti Jan 5 '17 at 17:24
  • 1
    Alias don't do parameter expansion - "$@" makes no sense there. Did you mean to make a function? – muru Jan 6 '17 at 2:57
  • Fair point; I don't use alias very often, so I forget that passing parameters is already handled automagically. – DopeGhoti Jan 6 '17 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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