Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open file in existing Vim instance instead of getting "existing swapfile " warning

I want Vim to reuse the currently existing instance if it exists. Usually, Vim pops up a warning about an existing swap file. Specifically, this is for switching between Vim and Visual Studio. (I know about ViEmu, but it doesn't work with Visual Studio Express.)

like image 628
Leftium Avatar asked Dec 05 '09 18:12

Leftium


1 Answers

Poster's Solution:

Solution: There is a plugin in the standard distribution of Vim: runtime/macros/editexisting.vim. Just copy it to the Vim plugins directory.

Extra details for Visual Studio Integration: Follow these steps to add Vim as an external tool and assign a convenient keyboard shortcut:

  • Title: Vim
  • Command: C:\Program Files\Vim\vim70\gvim.exe
  • Arguments: +$(CurLine) "$(ItemPath)"
  • Initial directory: $(SolutionDir)

Note I use slightly different settings so the cursor is set to the column from VS and centered in Vim:

  • Arguments: +"call cursor($(CurLine),$(CurCol))" +"normal zz" $(ItemPath)
  • Initial directory: $(FileDir)

Then set VS to automatically load chanages made from Vim:

In order to effectively use the two together and make sure .NET does not complain about its files changing, goto Tools > Options > Environment > Documents and ensure these two options are checked: Detect when file is changed outside the environment. Auto-load changes (if not currently modified inside the environment).

Finally set Vim to automatically load changes made from VS:

:set autoread


My Solution

Similar, but subtly different: Save this to a .settings file and import. Uses --servername and --remote-call to reuse existing Vim, tailored for the current solution.

<UserSettings>
    <ApplicationIdentity version="8.0"/>
    <ToolsOptions/>
    <Category name="Environment_Group" RegisteredName="Environment_Group">
        <Category name="Environment_ExternalTools" Category="{E8FAE9E8-FBA2-4474-B134-AB0FFCFB291D}" Package="{DA9FB551-C724-11d0-AE1F-00A0C90FFFC3}" RegisteredName="Environment_ExternalTools" PackageName="Visual Studio Environment Package">
            <PropertyValue name="edit with v&amp;im.Command">gvim.exe</PropertyValue>
            <PropertyValue name="edit with v&amp;im.Arguments">--servername $(SolutionFileName) --remote-silent +"call cursor($(CurLine),$(CurCol))" "$(ItemFileName)$(ItemExt)"</PropertyValue>
            <PropertyValue name="edit with v&amp;im.InitialDirectory">$(ItemDir)</PropertyValue>
            <PropertyValue name="edit with v&amp;im.SourceKeyName"/>
            <PropertyValue name="edit with v&amp;im.UseOutputWindow">false</PropertyValue>
            <PropertyValue name="edit with v&amp;im.PromptForArguments">false</PropertyValue>
            <PropertyValue name="edit with v&amp;im.CloseOnExit">false</PropertyValue>
            <PropertyValue name="edit with v&amp;im.IsGUIapp">true</PropertyValue>
            <PropertyValue name="edit with v&amp;im.SaveAllDocs">true</PropertyValue>
            <PropertyValue name="edit with v&amp;im.UseTaskList">false</PropertyValue>
            <PropertyValue name="edit with v&amp;im.Unicode">false</PropertyValue>
            <PropertyValue name="edit with v&amp;im.Package">{00000000-0000-0000-0000-000000000000}</PropertyValue>
            <PropertyValue name="edit with v&amp;im.NameID">0</PropertyValue>
        </Category>
    </Category>
</UserSettings>
like image 190
John Weldon Avatar answered Oct 01 '22 07:10

John Weldon