Wednesday, October 8, 2008

Vim to and from Visual Studio

This is the seventh post in my series on using Vim to do C# development. You can read the introduction into why someone might want to do that here.

Now that we've got Vim setup to a point where it's quite useful for doing C# development, we need to start actually using it. As I've discussed in the past, there will still be lots of times when you'll want to use Visual Studio. Whether it be for the Designer, or for Exploring APIs you're unfamiliar with through Intellisense, or for manipulating resources, settings, project, or solution files, or for debugging.

VS -> Vim
The key is to be able to get back and forth between them easily. To get from Visual Studio to Vim is the easiest. Simply add an "external tool" which launches Vim and opens the current file. In VS go to Tools -> External Tools. Click Add and enter the following:
Title: Vim
Command: C:\Program Files\Vim\vim70\gvim.exe
Arguments: +$(CurLine) "$(ItemPath)"
Initial directory: $(SolutionDir)

There will now be a "Vim" item in your Tools menu which will open Vim to the current file AND the current line.

You can assign a shortcut key to this command as follows.
  1. Note exactly where the "Vim" command appears in the Tools menu (How many items from the top or bottom is it?)
  2. Go to Tools -> Customize -> Toolbars
  3. Click on the Tools menu so it opens and find where the Vim tool was, it will now say External Command X where X is some number. Remember X.
  4. Back on the Customize window, click "Keyboard..."
  5. Type "ExternalCommandX" into the "Show commands containing" box, where X is the number you just found
  6. Choose your shortcut key and assign it. I use Ctrl+Shift+V, Ctrl+Shift+V
Now you can open Vim from Visual Studio with a simple shortcut key sequence.

Vim -> VS
To get from Vim to Visual Studio is pretty easy as well. Assuming that your current path (:cd) in Vim is the path containing the .sln file, you can simply type the following:
:! *.sln

When you hit the tab key Vim will automatically expand to the full name of the sln file. Hit enter and the solution will open in Visual Studio.

4 comments:

  1. Hey, i would like to say this is a wonderful blog.

    All that you have said in this blog is true, in case you are working on a project from the scratch and may not be true when working on the huge project(some thing like legacy project).

    This applies only for fresher or one with less experience. For experienced people it must not make a difference.

    VIM is the best editor every developed.

    ReplyDelete
  2. These days we have tools like ViEmu to be grateful for :)

    ReplyDelete
  3. Very helpful blog!

    I found it looking for TFS-Vim related info. Thanks for that.

    New to TFS, happy Vimmer >10jrs.

    Changing Title: "&Vim" will enable default windows shortcut usage for T,V

    Consider $(ItemDir) option for initial dir.

    And for arguments:
    --servername VimualStudio --remote-silent +"call cursor($(CurLine),$(CurCol))" $(ItemFileName)$(ItemExt)

    ReplyDelete