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
#regiontags. 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:\myproductFor 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:\myproductDone. 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.
-
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#CompleteThis will tell vim to guess the words based on C# syntax.

- Tags based completion: Ctrl-x Ctrl-]
-
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:\ %mCan you figure out a way to build C# projects in the commandline? (Hints: Vim :help make, search online for msbuild, devenv /help).
Happy Vimming
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
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.
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.
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.
Pingback: Code Kata Setup - Louis Salin's Blog - Los Techies : Blogs about software and anything tech!
thanks for sharing,,,
Did you ever finish that reflection tool? It would be so nice to be able to do that. These guys btw did write a tool like that but it has been unsupported for years. Maybe you can take that source and make it more modern?
http://insenvim.sourceforge.net/
Garber, I did spend some cycles on getting something up. But then I crossed by VsVim for Visual Studio 2010, and vi mode for monodevelop which are working pretty well; and provide intellisense natively
The one thing I haven’t been able to do is get quickfix to work with NAnt output (rather than msbuild or just csc). I’ve tried creating an expression for it in the compiler .vim file but every time I do I just lose patience. And there’s nothing to be found (apparently) on the internet that might help. I even tried adapting one example I found for Ant, which is what NAnt is based on, and has similar input, but no dice.
Hey, what color scheme is that in the screenshots?
It’s Sienna (http://www.vim.org/scripts/script.php?script_id=1259).