Calling gvim from MS Visual Studio
From Vim Tips Wiki
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
Tip 716 Previous Next created May 13, 2004 · complexity basic · author mosh · version 6.0
[edit] Sending File to External Vim Window
To bring up gvim from VC++ conveniently (which I do all day, so I ended up mapping these to function keys with gvim62):
In VC++ do menu->tools->customize->tools:
This allows single gvim window to open any file from msdev:
Name=sendtovim Command=sendtovim.exe Argument=<c-z>:cd $(FileDir)<CR>:e $(FilePath)<CR>:$(CurLine)<CR> Initial Dir=$(FileDir)
More recent versions of Vim do not include sendtovim.exe. In that case, you need to use the --remote option below.
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.
Other possible commands you may like are:
To open a new gvim for each file:
Command=gvim.exe Arguments=+$(CurLine) "$(FilePath)"
Note: Use quotes when your path/files have spaces in them.
Arguments=:cd $(FileDir)<CR>:e $(FilePath)<CR>:$(CurLine)<CR>/\<$(CurText)\><CR>$(CurCol)|:
This will open Vim on the definition of the word under text, for this you need to have tags file in your source tree:
Arguments=":cd $(FileDir)<CR>:ta $(CurText)<CR>/$(CurText)"
This is the client-server way:
Arguments=--remote +/"\%$(CurLine)l\%$(CurCol)v" $(FilePath)"
This lets you launch Vim with a single key:
- In VC++ do menu->tools->customize->keyboard->Tools->UserTool1 Current-key=<Press Pause>
To keep files in sync:
- msdev->tools->options->editor select
- save files before running tools
- Automatically load externally modified files.
- Unselect prompt before saving file.
In vimrc to update externally modified files:
:set autoread
[edit] Using Vim as an Internal (MDI) Window
The -P option allows you to launch gVim inside an application supporting MDI windows (such as Microsoft Visual C++). This option is somewhat buggy still, and can crash Visual Studio 6.0 if you try loading Vim with documents open in the default editor. To use it, add the following to the tools menu in Visual Studio:
Command: C:\Program Files\Vim\vim71\gvim.exe Arguments: -P "Microsoft Visual C++" --servername MDI_VIM
Name it something like "&Internal Vim" - you can now access it through the tools menu. Unlike the "Send To Vim" tool defined in the main part of this tip, you will need to use Vim's built-in file opening commands.
Notes:
- If you are using Visual Studio 6.0, launch the Internal Vim tool with no documents open in the default editor, and be sure to close Vim before opening any documents in the default editor, or Visual Studio will crash. Visual Studio .NET does not have this problem.
- The server name is unnecessary but prevents you from accidentally sending documents to it from external sources if you use the --remote series of commands (or allows you to specify your MDI Vim instance if that's what you want to do).
- The "Microsoft Visual C++" corresponds to the window title, so yours may vary. There may be an automatic way to get the window title similar to $(FilePath) above, but I don't know it.
- Visual Studio.NET handles an MDI Vim window much better, especially if you set the "MDI Environment" setting in Tools->Options->Environment->General. With .NET, you can actually launch the selected file in an internal Vim window automatically by adding +$(CurLine) "$(ItemPath)" to the end of the "arguments" field above. --remote-tab-silent can also be used if desired.
- The MDI window created by this call will not automatically gain input focus. Make sure Vim has focus before entering any commands!
[edit] References
[edit] Comments
TO DO
- Needs *lots* of cleanup to be useful - dodgy perhaps?
- Merge with tips as marked - the other ones provide similar solutions, but are focused on .NET versions of Visual Studio.
File vim71/doc/version5.txt (distributed with Vim 7.1) includes note:
- Included the "SendToVim" and "OpenWithVim" programs in the OleVim directory.
File vim71/doc/version6.txt includes note:
- Removed the OleVim directory and SendToVim.exe and EditWithVim.exe from the distribution.
- Can now use "gvim --remote" and "gvim --remote-send", which is portable.
That is, current versions of Vim should not use SendToVim. Instead, use the --remote gvim command-line argument. See :help client-server.
visvim.dll for MSVC 6.0 gives additional buttons for using gvim from MSVC in addition to or as replacement of the built-in editor.
See the vim71/VisVim directory for files README_VisVim.txt and VisVim.dll.
