Tip #580: Switching back and forth between ViM and Visual Studio .NET
tip karma |
Rating 69/24, Viewed by 7387
|
created: |
|
October 6, 2003 10:43 |
|
complexity: |
|
intermediate |
author: |
|
Francois Leblanc |
|
as of Vim: |
|
6.0 |
This tip is for when you work on a devstudio project and need the debugger heavily and/or can't stay in ViM all the time. But when it comes time to make changes you want to do them in ViM and don't want to relocate the file and line number.
After you have made the change and perhaps opened another file or navigated your way to a new section of the code you want to switch back to devstudio at the spot you were in ViM. It may be because you want to set a breakpoint or any reason.
The easy part:
Launching ViM from DevStudio.NET is easy.
From the DevStudio menu item Tools|External Tools... add a new entry where:
The "Command Line" field is set to the path of the ViM executable
The "Arguments" field contains: --servername gmain --remote-silent +$(CurLine) +"normal zz" $(ItemPath)
The "Initial Directory" may optionally contain: $(ItemDir)
This will start a ViM session or connect to an already existing one (--remote-silent) named gmain (--servername gmain). This will use only one instance of ViM for all devstudio editing. It will open the file specified by $(ItemPath) and set the cursor pos to $(CurLine). It will also execute the normal command zz to center the cursor.
You can then create a keyboard shorcut to map to this tool (Tools|Options||Environment|Keyboard, select Tools.ExternalCommandX) and you will be able to switch to ViM quickly.
The hard part:
Opening a file in an existing DevStudio.NET instance is a pain and setting the cursor to a line number is even more so.
DevStudio cannot be controlled by the command line. To open a file in an existing instance a DDE call must be initiated. Its an old and obsolete technology called Dynamic Data Exchange used for interprocess communication. When you click on a .cpp file in the Windows Explorer it calls devenv.exe with the /DDE switch (its undocumented) and sends it an Open DDE command. You can see it for yourself if you look at the file type mapping of .cpp in the Windows Explorer (if you haven't already changed them to open ViM :-)). The Explorer shell is DDE enabled but I found no way to send DDE from the command line (I didn't really look for it either ;-)). So I wrote a small C++ console app from the code I got from an Experts Exchange question. I formatted the code, renamed references from DevStudio to DevEnv and put it in a project.
Setting the line number is a different problem. I wrote a Perl script using the Win32::GuiTest module. This module allows interacting with the Windows GUI and provides a very useful function called SendKeys. The script finds the Visual C++ window (if you are using a different language change the script) and sends it: a CTRL-G, the current line number as specified on the command line and ENTER.
It is integrated in ViM by a function (in _vimrc) that gets the current file name and line number and silently executes the script:
function! DevEnvDDE()
let cmd = '!devenvdden.pl %:p ' . line(".")
silent execute cmd
endfunction
All that is left is to map the function to a key.
You can get the source files for the Perl script and DDE project at http://dunderxiii.tripod.com/vimtips/devenvdde.zip
The original DDE code was taken at http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20489782.html
Win32::GUITest is located at http://groups.yahoo.com/group/perlguitest/
<<Cut&Paste; without too much newlines, eg. into WORD |
Using vim to view source and edit textarea in mozilla/firebird >>
Additional Notes
|