Vim Tips Wiki
Register
No edit summary
Tag: Visual edit
 
(4 intermediate revisions by 3 users not shown)
Line 4: Line 4:
 
|previous=955
 
|previous=955
 
|next=958
 
|next=958
|created=July 7, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Jonathan Orlev
 
|author=Jonathan Orlev
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
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 me, the most irritating thing when using GVim/MacVim is starting a second copy of GVim/MacVim while a first one was already running.
  +
  +
Use [https://github.com/susam/vimer Vimer] tool to ensure that whenever a new file is opened from terminal, it opens as a new buffer in an existing GVim/MacVim window (not a new instance of GVim/MacVim). This script is available for Windows, Linux and macOS. It works with both GVim and MacVim. You can download the script and just rename it to <code>vi</code> (on Linux or macOS) or <code>vi.cmd</code> (on Windows) and place the script in a directory that appears in the <code>PATH</code> environment variable. Then just run <code>vi foo.txt</code> on the terminal and Vimer will ensure that the new file is always opened in a buffer of an existing instance of GVim/MacVim (not a new one). If you would rather want to open the files as new tabs (not buffers) in existing instance of GVim/MacVim, then use the <code>-t</code> option with Vimer like this: <code>vi -t foo.txt</code>. Read Vimer's [https://github.com/susam/vimer#getting-started Getting Started Guide] for more details.
   
 
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).
 
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).
Line 36: Line 38:
 
For interactive shell sessions, it would help to define a shorter alias, such as:
 
For interactive shell sessions, it would help to define a shorter alias, such as:
 
<pre>
 
<pre>
alias gvimrem='/usr/local/bin/gvim --remote-silent'
+
alias gvimrem='/usr/local/bin/gvim --remote-silent' </pre>
  +
</pre>
 
  +
However, running <code>gvimrem</code> alone would lead to an error because the <code>--remote-silent</code> option must always be followed by one or more filenames.
  +
  +
There is a script called [https://github.com/susam/vimer#readme Vimer] that takes care of these corner cases and opens files in existing instance of GVim or MacVim.
  +
  +
# <code>vimer foo.txt bar.txt</code> opens both files in the same instance of GVim or MacVim.
  +
# <code>vimer</code> launches a new instance of GVim or MacVim.
  +
# <code>ls | vimer -</code> shows the output of ls in an existing instance of GVim or MacVim if it exists.
  +
  +
For more convenience, consider renaming the script to a command you already use, e.g. <code>vi</code>, <code>gvim</code>, etc. On Windows, you have to rename it to <code>vi.cmd</code>, <code>gvim.cmd</code>, etc. This means that you don't have to retrain your muscle memory to use a new command and you can continue typing the command you are used to.
   
 
----
 
----
Line 50: Line 61:
 
</pre>
 
</pre>
   
I agree, this serverlist() functions is working on Linux, where v:servername does not. Replace the original!
+
I agree, this serverlist() functions is working on Linux, where v:servername does not. Replace the original!
   
 
----
 
----
Line 56: Line 67:
   
 
----
 
----
See the <tt>:RemoteOpen</tt> 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.
   
 
----
 
----

Latest revision as of 14:06, 10 February 2020

Tip 957 Printable Monobook Previous Next

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


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

Use Vimer tool to ensure that whenever a new file is opened from terminal, it opens as a new buffer in an existing GVim/MacVim window (not a new instance of GVim/MacVim). This script is available for Windows, Linux and macOS. It works with both GVim and MacVim. You can download the script and just rename it to vi (on Linux or macOS) or vi.cmd (on Windows) and place the script in a directory that appears in the PATH environment variable. Then just run vi foo.txt on the terminal and Vimer will ensure that the new file is always opened in a buffer of an existing instance of GVim/MacVim (not a new one). If you would rather want to open the files as new tabs (not buffers) in existing instance of GVim/MacVim, then use the -t option with Vimer like this: vi -t foo.txt. Read Vimer's Getting Started Guide for more details.

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' 

However, running gvimrem alone would lead to an error because the --remote-silent option must always be followed by one or more filenames.

There is a script called Vimer that takes care of these corner cases and opens files in existing instance of GVim or MacVim.

  1. vimer foo.txt bar.txt opens both files in the same instance of GVim or MacVim.
  2. vimer launches a new instance of GVim or MacVim.
  3. ls | vimer - shows the output of ls in an existing instance of GVim or MacVim if it exists.

For more convenience, consider renaming the script to a command you already use, e.g. vi, gvim, etc. On Windows, you have to rename it to vi.cmd, gvim.cmd, etc. This means that you don't have to retrain your muscle memory to use a new command and you can continue typing the command you are used to.


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.