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?

2
  • 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. Commented Jan 5, 2017 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. Commented Jan 5, 2017 at 17:24

1 Answer 1

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'
4
  • 3
    You could drop the absolute path and use command vim instead. And $@ needs to be double quoted. Commented Jan 5, 2017 at 17:21
  • Noted and incorporated. Commented Jan 5, 2017 at 17:24
  • 1
    Alias don't do parameter expansion - "$@" makes no sense there. Did you mean to make a function? Commented Jan 6, 2017 at 2:57
  • Fair point; I don't use alias very often, so I forget that passing parameters is already handled automagically. Commented Jan 6, 2017 at 15:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.