5

Is there any way I can use Vim with or Neovim to edit my project, and still be able to build it using Visual Studio?

Note I am not talking about limited Vim emulators for Visual Studio but proper integration, with Vim at the front-end as the main tool and build tools at the back-end of the pipeline.

3

So, what you're asking about is totally possible. Your code is, ultimately, going to consist of a bunch of plain text files (with possibly some assets that are binary, but don't need to be editable). Since that is the case, you can always edit your source code with Vim, and then open Visual Studio to compile/build your project. But, I don't think that's really what you're asking about.

If you want to integrate the C compiler released with Visual Studio with your source code, this wouldn't be so hard. You can map F9 to the command !cl % which would use the cl compiler released with Visual Studio to compile the working file (I'm sure you can spice that up for passing the proper command line options). Another option would be to use this solution to set the compiler for all filetype of extension .prog. That might be a bit more of 'idomatic vim`.

Depending on your requirements, this functionality could be obtained with other languages using Visual Studio. But, you would need to be clearer regarding your particular requirements. In the simplest case, you can use the solution posted in the first paragraph.

Edit

If you're okay with installing a plugin, Tim Pope (@tpope) produced a great plugin, vim-dispatch, that can asynchronously compile projects and do some other fancy things, like parsing the error report generated by the compiler and automatically opening the quickfix window with all detected warnings/errors. You can easily configure which compiler you would like to use for a given file(type). That seems to be a really good way to go

If you're vacillating in regards to using this plugin, I highly recommend watching this video which @tpope released, demonstrating the power of vim-dispatch.

  • Is this a bit like the dispatch.vim plugin? I mean, assign the :make command to use the CL Visual Studio compiler and capture the errors, and feed them in the quickfix window? – mbilyanov Apr 3 '16 at 17:00
  • @symbolix, my suggestion is worse than using Tim Pope's (@tpope) dispatch.vim plugin. I'll edit my answer to include that as the preferred way to integrate compiler support with vim. It's ability to do work asynchronously is particularly attractive. – fuzzybear3965 Apr 3 '16 at 18:00
3

External Editing

If you have Visual Studio up and running while you edit the project files in a separate window under Vim, every time you switch back to Visual Studio, it looks at the on-disk timestamps for every file you have open in Visual Studio, including the project file. For each file that changed since you switched away from Visual Studio, it asks if you want to reload it from disk.

It even asks if you want to just reload everything that's changed, so you can edit multiple files without Alt-Tabbing back to Visual Studio after each edit.

Building from Vim

If you're using Visual Studio 2010 or newer, they've switched to MSBuild for Visual C++ projects. (Prior to that, MSBuild was only for projects in .NET languages.) That means you can integrate MSBuild into Vim as a build system:

:set errorformat=\ %#%f(%l\\\,%c):\ %m
:set makeprg=msbuild\ /nologo\ /v:q\ /property:GenerateFullPaths=true

(Swiped from here.)

Visual-Studio-Like Command Key Mappings

These command mappings are inspired by old versions of Visual Studio:

map <S-F4> :cp<cr>
map <F4> :cn<cr>
map <F5> :make run<cr>
map <F7> :make<cr>

Microsoft keeps changing these command keys, and I keep switching them back when installing new versions, so I can't remember what they're using these days. Adjust to taste.

Emulating Vim Inside Visual Studio

Now, there's another way to go entirely here: emulate Vim inside Visual Studio!

This was once the province of only those who bought Visual Studio Professional or higher, but with the release of Visual Studio 2015, the free version finally has the ability to load third-party extensions. There are a couple of them that do this:

  • VsVim, a free extension available through the Visual Studio Gallery

  • ViEmu, a commercial extension. Presumably it's better in some way, else they wouldn't still be in business.

Or, you can go join the cool kids and start using Visual Studio Code, which has at least three Vim emulator extensions. I haven't test-driven them extensively, but here they are ordered by how well they matched my expectations of how a Vim emulation should behave:

To install any of them, just hit Ctrl-Shift-P (Windows) inside Visual Studio code, type inst, select Extensions: Install Extension, then type vim. All three will come up, along with a few other things which are not Vim emulators for VSCode.

None of those extensions are flawless.

(I say this as one who uses Vim daily, and installs such extensions only to ease the transition when I do occasionally have to use Visual Studio and VSCode.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.