C# and VIM

Posted on Fri 10 April 2009 in Technology • 3 min read

Let’s explore some of the tweaks to improve C# experience in Vim. All of the tips in this article can be applied to .Net Framework/Windows and Mono/Linux combination.

  • Code folding ——————

    The default syntax mode code folding doesn’t play well with C#. It allows folding of only the code between the #region tags. Alternatively we can take advantage of the indentation in the source code to make vim aware of which parts of the code to fold. Put the following snippet in your vimrc:
    " Folding : http://vim.wikia.com/wiki/Syntax-based_folding, see comment by Ostrygen au FileType cs set omnifunc=syntaxcomplete#Complete au FileType cs set foldmethod=marker au FileType cs set foldmarker={,} au FileType cs set foldtext=substitute(getline(v:foldstart),'{.*','{...}',) au FileType cs set foldlevelstart=2 

  • Code browsing ——————-

    This is yet another important functionality when it comes to dealing with bulky libraries. We will use the awesome tool: Exuberant Ctags. With ctags we will scan the code base and create a tags file. And then we will make vim aware of the tags to help us autocomplete. Let’s start by scanning all c# files in d:\myproduct directory and creating the tag file d:\mytagfile.

In a powershell window, navigate to the ctags installation directory and type in:
.\ctags.exe --recurse -f d:\mytagfile --exclude="bin" --extra=+fq --fields=+ianmzS --c#-kinds=cimnp d:\myproduct

For details on the various options used above, please see the ctags man page.

Next, let’s make vim aware of our tag file. In your vimrc, include the following lines:
set tag = d:\myproduct

Done. Now let’s try out the navigation:
Place cursor on any identifier in source code and press Ctrl-]. This will take you to the definition of that identifier. Sometimes there can be more than places where an identifier is defined (in different source context ofcourse). In such cases, use the shortcut g] to see a list of matching locations for a particular identifier. Vim will show you all matching identifiers and ask you where you want to navigate.

1
![](http://i73.photobucket.com/albums/i206/pratikarun/CSharpAndVim2.jpg)
  • Code completion ———————-

    Vim supports a number of word completion mechanisms (see :help completion). I’ll touch upon the relevant ones for C# (or general programming for that matter).  These keys are applicable in Insert or Replace mode.

    • Tags based completion: Ctrl-x Ctrl-]
      Here vim will suggest you various words based on the tag file included.
    • Current and included files: Ctrl-x Ctrl-i
      Vim will search for relevant words on all included files and the source file.
    • Omni completion: Ctrl-x Ctrl-o
      Here vim will guess what will be the word based on the context (Type, member etc..). If you’ve noticed, we defined the omnifunc for C# in Code folding section:
      au FileType cs set omnifunc=syntaxcomplete#Complete

    This will tell vim to guess the words based on C# syntax.

    Tag completion in
VIM

  • Quickfix ————

    Here’s a small tip to make vim recognize the error messages thrown by the commandline build tool msbuild. Put this in your vimrc:
    " Quickfix mode: command line msbuild error format au FileType cs set errorformat=\ %#%f(%l\\\,%c):\ error\ CS%n:\ %m

Can you figure out a way to build C# projects in the commandline? (Hints: Vim :help make, search online for msbuild, devenv /help).

Happy Vimming :)