How to use vim as your IDE

Vim can be transformed into a powerful Integrated Development Environment (IDE) through the use of plugins and custom configurations. In this article we are going to use the latter to allow you to compile a project from vim

Once you are able to compile your code without having to leave the vim window, you can say that vim is your IDE. However, in order to do so, you will need to use a powerful vim feature called the Location List. It is a small window that pops up at the bottom of the current window, and we are going to use it for displaying the compilation result. If there are compilation errors, we will be able to navigate directly to them from the Location List window.

First, we need to setup the command that we are going to use for compiling the source code. For example, we can use nmake to compile a project in Windows:


set makeprg=nmake

Vim comes with some predefined patterns for the error formats of the compilers, so that vim can parse them and navigate from the error to the line of the specific file where the error is. However, if the error messages of your compiler are not recognized by vim, there is a variable called errorformat that allows you to specify the pattern of the error messages of your compiler. For example, the Windows C compiler error format is not automatically recognized by vim. But we can use the following pattern to allow vim to parse the error messages:


set errorformat=%f(%l):%m

Once we have the build command and the error format ready, we can trigger the compilation of the source code using the command system(&makeprg). And then load the result into the Location List window with the command lgetexpr. But this command just loads contents into the Location List buffer, to open the window we need to call the command lopen. As these are too many commands to by manually, we can create a vim command and a mapping, to trigger the compilation just with a key binding.


let mapleader=" "

command! MAKE lgetexpr system(&makeprg) | lopen

nnoremap <leader>m :MAKE<CR>

Finally, we can add a couple of auto commands to move around the Location List window, and navigate easily through the compilation errors.


autocmd FileType qf nnoremap <buffer> p :lprev<CR><C-w>w
autocmd FileType qf nnoremap <buffer> n :lnext<CR><C-w>w

This is the final vim configuration that you can add to your .vimrc:

And this is how it would look like in action:

vim as IDE




Popular posts from this blog

How to setup NeoVim configuration file

WebAssembly (Wasm): Fixing the Flaws of Applets

How to query Outlook from PowerShell