Vim Tips Wiki
m (Preventing a second copy of VIM from starting moved to Prevent a second instance of Vim from starting: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=957
 
|id=957
  +
|previous=955
|title=Preventing a second copy of VIM from starting
 
  +
|next=958
|created=July 7, 2005 4:28
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Jonathan Orlev
 
|author=Jonathan Orlev
 
|version=5.7
 
|version=5.7
 
|rating=4/11
 
|rating=4/11
  +
|category1=
|text=
 
  +
|category2=
Hello everybody.
 
 
 
 
For me, the most irritating thing when using the wonderful VIM is starging a second copy of VIM while a first one was already running.
 
 
For example: openning a file already open in the first VIM instance within a second instance of VIM gives an error message, because the swap file is already in use (Not to mention that this might cause a loss of data).
 
 
 
 
So I added this to my .vimrc. I don't know if this is the most elegant solution, but it works for me.
 
 
I am using GVIM on Windows, but this will probably also work with GVim in other OS's.
 
 
You may want to modify it so VIM exists after showing the warning message.
 
 
 
 
Finally, if someone is familiar with a better option (a built in maybe ?), please post a comment
 
 
 
 
" ----
 
 
" If the v:servername ends with a number, then this is for sure a second
 
 
" copy of VIM.
 
 
if v:servername =~? '^.*[0-9][0-9]*$'
 
 
echo "MyWarning: Another copy of GVIM or VIM is probably loaded !"
 
 
endif
 
 
" ----
 
 
}}
 
}}
 
For me, the most irritating thing when using the wonderful Vim is starting a second copy of Vim while a first one was already running.
   
 
For example: opening a file already open in the first Vim instance within a second instance of Vim gives an error message, because the swap file is already in use (not to mention that this might cause a loss of data).
== Comments ==
 
For users running GVim on Unixes, an alternative is to always use
 
/usr/local/bin/gvim --remote-silent FILENAME
 
If GVim is already running, the file is opened there. Otherwise, a new GVim instance is started.
 
   
 
So I added this to my vimrc. I am using gvim on Windows, but this will probably also work with gvim in other systems.
For interactive shell sessions, it would help to define a shorter alias, such as:
 
alias gvimrem='/usr/local/bin/gvim --remote-silent'
 
   
  +
<pre>
 
" If the v:servername ends with a number, then this is for sure a second
 
" copy of Vim.
 
if v:servername =~? '^.*[0-9][0-9]*$'
  +
echo "MyWarning: Another copy of gvim or Vim is probably loaded!"
 
endif
  +
</pre>
   
 
==Comments==
Dubhead &lt;echochamber at gmail&gt;
 
 
For users running gvim on Unixes, an alternative is to always use
, July 7, 2005 19:03
 
  +
<pre>
----
 
 
/usr/local/bin/gvim --remote-silent FILENAME
To Dubhead:
 
  +
</pre>
   
 
If gvim is already running, the file is opened there. Otherwise, a new gvim instance is started.
Thanks,
 
   
 
For interactive shell sessions, it would help to define a shorter alias, such as:
But according the documentation it only works with a file name argument, where what I showed also works when invoking VIM without file name argument.
 
  +
<pre>
 
alias gvimrem='/usr/local/bin/gvim --remote-silent'
  +
</pre>
   
I will have to read some more about this subject, because I saw the s:servername variable, but never quite understood the whole context it used in.
 
 
Also, I now see that I can use the function serverlist() to make the tip also work regardless of using VIM OR GVIM (I'll check if there are more than one item in the returned list.
 
 
JonathanOrlev_REMOVEME_--AT--yahoo.com
 
, July 10, 2005 3:29
 
 
----
 
----
 
Here is a better version for the tip. This is more general, and therefore preferable:
Everyone who needs this tip, please read:
 
   
  +
<pre>
Here is a better version for the tip. This is more general, and therefore preferable:
 
 
" If the serverlist contains more than one item, than another instance of
 
" Vim is loaded.
 
if serverlist() =~? "\n."
 
echo "MyWarning: Another copy of gvim or Vim is probably loaded !"
 
endif
  +
</pre>
   
  +
I agree, this serverlist() functions is working on Linux, where v:servername does not. Replace the original!
" ---
 
   
" If the serverlist contains more than one item, than another instance of
 
" VIM is loaded.
 
if serverlist() =~? "\n."
 
echo "MyWarning: Another copy of GVIM or VIM is probably loaded !"
 
endif
 
 
" ---
 
 
JonathanOrlev_REMOVEME_--AT--yahoo.com
 
, July 10, 2005 4:01
 
 
----
 
----
 
And you may also replace the operator '=~?' with '=~'
 
And you may also replace the operator '=~?' with '=~'
   
JonathanOrlev_REMOVEME_--AT--yahoo.com
 
, July 10, 2005 4:32
 
----
 
Is ther a way to open a file in a new gvim instance, unless it's already opened?
 
 
Thanks
 
Ronnie
 
 
vt.10.ronnielj--AT--spamgourmet.com
 
, July 11, 2005 6:53
 
 
----
 
----
See the :RemoteOpen command from remoteopen.vim which is part of latex-suite plugin
+
See the <code>:RemoteOpen</code> command from remoteopen.vim which is part of latex-suite plugin.
   
Aditya Mahajan
 
, July 14, 2005 8:11
 
 
----
 
----
<!-- parsed by vimtips.py in 0.508852 seconds-->
 

Revision as of 05:59, 13 July 2012

Tip 957 Printable Monobook Previous Next

created 2005 · complexity basic · author Jonathan Orlev · version 5.7


For me, the most irritating thing when using the wonderful Vim is starting a second copy of Vim while a first one was already running.

For example: opening a file already open in the first Vim instance within a second instance of Vim gives an error message, because the swap file is already in use (not to mention that this might cause a loss of data).

So I added this to my vimrc. I am using gvim on Windows, but this will probably also work with gvim in other systems.

" If the v:servername ends with a number, then this is for sure a second
" copy of Vim.
if v:servername =~? '^.*[0-9][0-9]*$'
  echo "MyWarning: Another copy of gvim or Vim is probably loaded!"
endif

Comments

For users running gvim on Unixes, an alternative is to always use

/usr/local/bin/gvim --remote-silent FILENAME

If gvim is already running, the file is opened there. Otherwise, a new gvim instance is started.

For interactive shell sessions, it would help to define a shorter alias, such as:

alias gvimrem='/usr/local/bin/gvim --remote-silent'

Here is a better version for the tip. This is more general, and therefore preferable:

" If the serverlist contains more than one item, than another instance of
" Vim is loaded.
if serverlist() =~? "\n."
  echo "MyWarning: Another copy of gvim or Vim is probably loaded !"
endif

I agree, this serverlist() functions is working on Linux, where v:servername does not. Replace the original!


And you may also replace the operator '=~?' with '=~'


See the :RemoteOpen command from remoteopen.vim which is part of latex-suite plugin.