Thursday, October 2, 2008

Vim Intellisense

This is the third 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.

Before you get excited, I can't tell you how to get Intellisense setup in Vim. I can just tell you how to get close. I would say close enough, but you'll be the judge.

Intellisense in Visual Studio can do the following things:
  1. Complete variable, class, and method names as you type them
  2. Show you what methods/properties/etc are available on a given class after you type "class."
  3. Show you summary documentation on methods/properties/classes/parameters
Visual Studio can also take you to the code that defines a class/method/property with "Go To Definition".

In Vim, I can do #1 (sort of) and "Go To Definition." I claim this is "good enough" in most circumstances because Go To Definition takes you to the code where the method documentation resides AND it makes it easy to get right back to where you came from. This is obviously much more work than it is in Visual Studio where all that information is simply at your finger tips, but I found that the majority of the time the information is in my memory. And when it isn't, going to the definition and then coming back isn't so costly that it is a deal breaker, at least for me.

But in the interest of being very plain and transparent about this, Vim can't even come close to touching Intellisense, and there are many times when Intellisense makes life a lot easier. In these times, I work in Visual Studio. With that out of the way, on to how to do this in Vim!

The first part, Word Completion, is built in to Vim and you don't have to do anything at all to turn it on. Simply hit <ctrl+n> and Vim will autocomplete the word you're typing. If there is more than one match, it will show them in a popup menu. Use <ctrl+n> and <ctrl+p> to move forward and backward through that popup. Type anything to select the word. Vim uses every word in every buffer you've opened (in the current session) to match against, so it will always work on variable and method names in the same file and will work on those in other files if you've opened that file.

The second part, Go To Definition, requires more work. First you have to get a program called ctags. When you run ctags (from the shell) it builds a "tags file" which is basically a dictionary of every method and class name in every file you told it to parse. Vim knows how to parse a tags file, so you can then position your cursor on a method name in Vim and type <ctrl+]>, and Vim will take you to the file and line where that method is defined. After you've found what you need and are ready to return to where you came from simply type <ctrl+t>.

To run ctags just go to the top directory of your source code and run:
ctags --recurse
This will parse all the files in that directory and all it's subdirectories and then create a "tags" file in that directory. By default Vim will look for tags files in Vim's current directory and in the directory where the file you are editing is located. The only trick here is that you have to keep the tags file up to date by manually re-running ctags if you add or remove methods.

3 comments:

  1. I like to add the following options to my ctags invocation:

    --extra="+fq"

    +f creates a tags entry for the file itself, useful to jump to a file

    +q adds class-qualified tags for each member of a class. The ctags docs say that this is only implemented for C++, Java and Eiffel. It works in C#, I presume, because C#'s syntax is close to Java in this respect.

    --fields="+Si"

    +S causes signature info to show up in :tselect

    +i causes inheritance info to show up in :tselect

    ReplyDelete
  2. Just curious, have you tried "Intellisense for Vim"
    http://www.vim.org/scripts/script.php?script_id=747

    Also, since MS released the .NET Framework source code, it might be nice to tie that in as well.

    I tried your "make" tips using csc. For some reason it hasn't worked for me yet, but I'll keep at it.

    PS
    I'm really pleased someone is writing this post. I'm a VS user, but sometimes I want something lightweight. I'm new to vim, so this is a big help. I plan on using ViEmu in VS so that I can get these keys mapped into my brain!

    -J

    ReplyDelete
  3. anonymous: I haven't used Intellisense for Vim, but I read that it was very out of date so I never bothered trying it.

    If you're using csc, you might look at the "cs" compiler that comes with Vim7 (tools -> SeT Compiler -> cs)

    ReplyDelete

Note: Only a member of this blog may post a comment.