DEV Community

Cover image for Setting Up Razor Support in Neovim for Enhanced C# Development
ben ajaero
ben ajaero

Posted on

Setting Up Razor Support in Neovim for Enhanced C# Development

When working with C# in web development scenarios, especially when dealing with ASP.NET Core projects, having support for Razor can significantly improve your productivity. Fortunately, Neovim’s flexibility allows you to set up a development environment tailored to your needs. Here’s how you can integrate Razor support into your Neovim setup.

Step 1: Install the vim-vsnip Plugin

First things first, a snippet plugin can be a real time-saver. vim-vsnip is a popular choice among Neovim users. To install it using vim-plug, add this line to your init.vim:

Plug 'hrsh7th/vim-vsnip'
Enter fullscreen mode Exit fullscreen mode

https://github.com/hrsh7th/vim-vsnip

Afterwards, run :PlugInstall in Neovim to get the plugin installed.

Step 2: Add vim-csharp to Neovim

The vim-csharp plugin adds nice enhancements for C# development. To include it in your toolset, simply place this line in your init.vim under the plugins section:

Plug 'OrangeT/vim-csharp'
Enter fullscreen mode Exit fullscreen mode

https://github.com/OrangeT/vim-csharp

Save your changes and run :PlugInstall to finalize the addition.

Step 3: Locate Your Snippet Directory

vim-vsnip will need to know where to find your snippets. To discover the default snippet directory, issue the following command in Neovim:

:echo(vsnip_snippets_dir)
Enter fullscreen mode Exit fullscreen mode

Note down the directory path that appears—it’s typically found at ~/.vsnip.

Step 4: Copy Snippet Files to the Right Place

Now, you need to copy the snippet files from vim-csharp to your vim-vsnip snippet directory. If you installed the plugin via vim-plug, they should be located in ~/.local/share/nvim/plugged/vim-csharp/snippets. Use this command in your terminal to move them over:

cp ~/.local/share/nvim/plugged/vim-csharp/snippets/*.snippets ~/.vsnip
Enter fullscreen mode Exit fullscreen mode

Adjust the paths as necessary if your setup differs.

Step 5: Configure Neovim for Razor Files

For Neovim to recognize .cshtml and .razor files and associate them with the proper filetype, include these lines in your init.vim:

autocmd BufNewFile,BufRead *.cshtml set filetype=html.cshtml.razor
autocmd BufNewFile,BufRead *.razor set filetype=html.cshtml.razor
Enter fullscreen mode Exit fullscreen mode

With these settings, Neovim will apply the correct syntax highlighting and other filetype-specific behaviors for Razor files.

You can also install Mason and Nvim-cmp if you would like those features.

By following through with these steps, you've now equipped Neovim with a sharper edge for C# and Razor development, paving the way for a more streamlined coding experience. Whether it’s crafting intricate Razor pages or diving deep into C# logic, your Neovim setup is now up to the task.

Top comments (2)

Collapse
 
kentvu profile image
Vũ Trần Kiên

I see vim-csharp hasn't been updated for 7 years, is it still alive? I have to use neovim-nightly due to roslyn-nvim :(

Collapse
 
alexzeitler profile image
Alexander Zeitler

Thanks, will give it a try!
How's the debugging experience?