Setting Up Razor Support in Neovim for Enhanced C# .Net Development

Ben Ajaero
2 min readJan 26, 2024

--

Photo by Uriel SC on Unsplash

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’

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’

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)

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

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

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 as well.

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.

--

--

No responses yet