Vim Tips Wiki
(Move categories to tip template)
(adjust previous/next navigation + minor manual clean)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
{{Duplicate|683|716|1253}}
 
 
{{TipImported
 
{{TipImported
 
|id=580
 
|id=580
 
|previous=579
 
|previous=579
 
|next=581
 
|next=581
|created=October 6, 2003
+
|created=2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Francois Leblanc
 
|author=Francois Leblanc
Line 13: Line 11:
 
|category2=Windows
 
|category2=Windows
 
}}
 
}}
  +
This tip is probably only useful for '''old versions of Visual Studio'''. For integrating Vim with modern versions of Visual Studio, see [[Integrate gvim with Visual Studio]].
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.
 
   
 
To open a file in an existing instance of some versions of Visual Studio, a DDE call must be initiated. It's 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 (it's 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 Visual Studio to DevEnv and put it in a project.
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:
 
:<pre>--servername gmain --remote-silent +$(CurLine) +"normal zz" $(ItemPath)</pre>
 
*The "Initial Directory" may optionally contain: <tt>$(ItemDir)</tt>
 
 
This will start a Vim session or connect to an already existing one (<tt>--remote-silent</tt>) named gmain (<tt>--servername gmain</tt>). This will use only one instance of Vim for all devstudio editing. It will open the file specified by <tt>$(ItemPath)</tt> and set the cursor pos to <tt>$(CurLine)</tt>. It will also execute the normal command <tt>zz</tt> 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. It's 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 (it's 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.
 
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:
 
It is integrated in Vim by a function (in vimrc) that gets the current file name and line number and silently executes the script:
 
 
<pre>
 
<pre>
 
function! DevEnvDDE()
 
function! DevEnvDDE()
Line 54: Line 31:
 
The original DDE code was taken at http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20489782.html
 
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/
+
Win32::GUITest is located at http://sourceforge.net/projects/winguitest/
   
 
==Comments==
 
==Comments==
I have the script send-to-msdev.sh, that uses Perl + Win32::Ole to open files in VC. You can even set breakpoints from perl, this script has pointers to MSDN documentation.
+
I have the script send-to-msdev.sh, that uses Perl + Win32::Ole to open files in VC. You can even set breakpoints from Perl, this script has pointers to MSDN documentation.
 
http://www.cs.albany.edu/~mosh/Perl/send-to-msdev.ksh
 
http://www.cs.albany.edu/~mosh/Perl/send-to-msdev.ksh
   
 
----
 
----
I udated the DevEnvDDE program to connect to Visual Studio .NET 2003. The code now supports an extra command line parameter that specifies the visual studio instance to open the file in (VS6, VSNET or VSNET2003). Simply download the new sources (same link).
+
I updated the DevEnvDDE program to connect to Visual Studio .NET 2003. The code now supports an extra command line parameter that specifies the visual studio instance to open the file in (VS6, VSNET or VSNET2003). Simply download the new sources (same link).
   
 
Note: it now defaults to Visual Studio .NET 2003 instead of Visual Studio .NET
 
Note: it now defaults to Visual Studio .NET 2003 instead of Visual Studio .NET
 
----
 
Just to let you know, to open a file with Visual Studio (At least the current versions) you simple can put the file name as an arument to the command line.
 
 
Example:
 
<pre>
 
"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv.exe" "BE-NotInDirectory\main.cpp" "CC-late\main.c
 
pp" "DO\Lab 5.cpp" "MG\Lab5.cpp" "NathanK\main.cpp" "NH\func.h" "NH\func.cpp" "NH\main.cpp" "NM-late\main.cpp"
 
</pre>
 
 
Would open all of these files.
 
 
----
 
For Visual Studio 6.0 replace ItemPath and ItemDir with FilePath and FileDir, as follows:
 
 
<pre>
 
Arguments: --servername gmain --remote-silent +$(CurLine) +"normal zz" "$(FilePath)"
 
(Optional) Initial directory: $(FileDir)
 
</pre>
 
 
----
 
An alternate distribution of Win32::GuiTest can be found at https://sourceforge.net/projects/winguitest/
 
 
Note: WaitWindow was a recent addition.
 
 
----
 
To get the current column as well as the current line:
 
 
<pre>
 
--remote-silent +"call cursor($(CurLine),$(CurCol))" +"normal zz" $(ItemPath)
 
</pre>
 
 
----
 

Latest revision as of 05:24, 11 August 2011

Tip 580 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Francois Leblanc · version 6.0


This tip is probably only useful for old versions of Visual Studio. For integrating Vim with modern versions of Visual Studio, see Integrate gvim with Visual Studio.

To open a file in an existing instance of some versions of Visual Studio, a DDE call must be initiated. It's 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 (it's 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 Visual Studio 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://sourceforge.net/projects/winguitest/

Comments[]

I have the script send-to-msdev.sh, that uses Perl + Win32::Ole to open files in VC. You can even set breakpoints from Perl, this script has pointers to MSDN documentation. http://www.cs.albany.edu/~mosh/Perl/send-to-msdev.ksh


I updated the DevEnvDDE program to connect to Visual Studio .NET 2003. The code now supports an extra command line parameter that specifies the visual studio instance to open the file in (VS6, VSNET or VSNET2003). Simply download the new sources (same link).

Note: it now defaults to Visual Studio .NET 2003 instead of Visual Studio .NET