Inside Out

Notes on seeking wisdom and crafting software.

C# and VIM

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.

" 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`   


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.


" 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 :)