created 2004 · complexity intermediate · author Jon Heiner · version 6.0
On Windows systems, there are several ways to integrate Vim with Microsoft Visual Studio.
Control Visual Studio from VimScript includes features:
- Compatible with Visual Studio 2003, 2005, 2008, and 2010
- Control Visual Studio from within Vim.
- Load the current Visual Studio file into Vim.
- Load the current Vim file into Visual Studio.
- Compile the current file (C/C++) within Vim.
- Build the current project (C/C++/C#) within Vim.
- Load results from Visual Studio into the Vim quickfix file.
IF YOU GET ERRORS, especially E887, you may need to copy some pywin32 files around to allow your system to find them. See http://code.google.com/p/vim-visual-studio/issues/detail?id=2 and http://stackoverflow.com/a/20017920/1390430.
Making Visual Studio's editor work like Vim
ViEmu for Visual Studio is commercial software that emulates Vim editing commands within Visual Studio (Vim is not used).
VsVim is an open source Vim emulator (code at https://github.com/jaredpar/VsVim ). It is only available for all editions of Visual Studio 2010, 2012, 2013 and 2015 excluding Express Edition, and as with viemu does not use Vim.
Vim as an external tool
If you like Vim and use MS Visual Studio .NET for debugging and want a lightweight way to open the file you're currently debugging in VS in Vim, you can add Vim as an external tool. This will let you use a single keystroke to open the current VS file in vim with the cursor at the same line and even at the same column.
The Tool: In Visual Studio, Tools > External Tools > Add:
--servername gVimStudio --remote-silent +"execute 'normal! $(CurLine)G$(CurCol)|'" "$(ItemFileName)$(ItemExt)"
NOTE: for visual Studio 2017 I needed to use:
--servername gVimStudio --remote-silent +"execute 'normal! $(CurLine)G$(CurCol)|'" "$(ItemPath)"
- Initial directory:
Note that, if you are using tabs for indenting, this will only place the cursor on the correct column if you have tabs set to display as the same width in both Vim and Visual Studio.
If you have other settings you'd like to apply (like normal zz to centre the cursor or updating path for :find, then you can put them in $HOME/vimfiles/visualstudioinvoke.vim and add +"runtime visualstudioinvoke.vim" before $(ItemFileName).
$HOME is either the %HOME% environment variable if one is defined, or Vim determines an appropriate directory which you can see by doing
:echo $HOME within Vim.
The Shortcut: In Visual Studio, Tools -> Options -> Environment -> Keyboard:
- Command: ExternalCommand1 (it's easiest if you move your Vim external tool to be the first, otherwise use the correct index).
- Shortcut key: F1
This will allow you to use your key combination to open the current file at the current line and cursor in a new vim browser. The browser will start at the directory of that file, so ':e .' will edit the directory of that file.
--servername VimStudio part if you want it to open a new instance of Vim each time.
Preventing Nagging: 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).
In your visualstudioinvoke.vim, enable autoread so vim will update files when they're modified in Visual Studio:
See Use Vim like an IDE for some useful plugins to help you stay in Vim longer.
Using old versions of Vim
Note: Only old versions of Vim include sendtovim.exe.
The Tool: In Visual Studio, Tools > Customize > Tools:
<c-z>:cd $(FileDir)<CR>:e $(FilePath)<CR>:$(CurLine)<CR>
- Initial directory:
Note: <c-z> brings gvim to foreground - on win2k, gvim gets focus but won't bring itself to foreground otherwise. You can remove it if you don't have this bring-to-foreground problem.
Visual Studio 2010
These arguments work better as for some reason the above ones break.
--remote-silent +"call cursor($(CurLine),$(CurCol))" $(ItemFileName)$(ItemExt)
Opening Vim file in Visual Studio
Much like Vim's
--remote, you can run devenv with the edit parameter to open a file in an existing Visual Studio. This command abbreviation opens the file you are currently editing, but doesn't transfer line or column postition.
:cabbrev vsedit :!start "c:\Program Files\Microsoft Visual Studio 8\Common7\ide\devenv.exe" /edit "%"
This is not the same as Embedding vim in Visual Studio.
- Open a solution in VS.Net.
- In the solution explorer (View->Solution or Ctrl + R), you should see a list of files (click on the "Solution Explorer" tab if you do not see the file list).
- Right click on any of the files and choose "Open With...", this brings up the Open With dialog.
- Click on "Add..." this brings up the Add Program dialog.
- Click on "Browse..." and point the Browse dialog to your gvim location.
- You have now added gvim to the list of editors, you may also select gvim to be the default editor for that file type.
You may need to do this for all file types you wish to edit with gvim. And yes you still have VS.Net open the file in its own editor.
Of course if you want to reverse changes simply remove gvim from the list of editors; but why would you want to do that?
VS.net opens the window in the middle of the screen and at the location and size of the its "source pane". How can I adjust where and of what size is the vim window when it opens? You can control your window size in your vimrc:
gui winpos 33 0 set lines=100
You can restrict this to files of a certain type by adding the above lines to a .vim file in your ftplugin directory. (For example in ~/.vim/ftplugin/cpp.vim for c++ files in visual studio.)
Editing in a single Vim
If VS.Net insists on opening each file in a new vim window, you may be able to force it to open each file in the same window by using a batch file instead of gvim to use vim's server-client functionality. Try this:
pathtovim\gvim.exe --servername VimStudio --remote-silent %*
If you use nmake, you can use
compiler msvc to set Vim's
makeprg for Visual C++. Use
compiler cs for C#.