Posted by: arun on: April 10, 2009
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.
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
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.
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.
au FileType cs set omnifunc=syntaxcomplete#Complete
This will tell vim to guess the words based on C# syntax.

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
You can generate tags from framework source(available to public from Microsoft, do a live search). The problem might be redundance.
With large C# codebase, I face the issue of the tags being redundant. And VIM tag search doesn’t use context for choosing a tag.
I’m not sure if ctags supports generating tags w/o file/line markers.
With a custom reflection tool, it should be easy to dump the types/methods in a format similar to the tag generated by ctags.
All the opinions (text and images) expressed in this website are mine. They do not represent the opinion of any living/non-living individual/groups/institutions, I am associated with.
The comments belong to the ip-address they generate from.
Albeo theme by Design Disease
May 19, 2009 at 4:42 pm
Any thoughts on a good way to use/generate ctags for the framework libraries as well?
The code completion also seems to be very dumb. I wonder if using the c++ omni complete would make things a bit better:
http://vim.wikia.com/wiki/C%2B%2B_code_completion
May 20, 2009 at 9:55 am
You wouldn’t be able to use code navigation for framework libs. However I’m looking at ways to get code completion for them. Current thought is to write a reflection app and then port java omnicomplete. Looks like an awesome way to spend a weekend
Thanks for the idea.
May 20, 2009 at 4:47 pm
Could ctags be generated from the framework source? Obviously it could but I’m not sure how well it would work, or if vim can except multiple tag files. I assume there’s a way to generate tag files without file/line markers.
There’s been a few attempt before that went down the reflection route but they all seem to get abandoned for some reason or other.
I’m just thinking out loud a bit now. I’m just starting to try to use vim as a full IDE after having used viemu for a few months.