3

I mainly use vim as an editor for C/C++ programming. Unfortunately, I'm not quite satisfied with the way my build process works. I know that it's possible to type in (or map to a key) :make to run the make process. I dislike the way this command works, though, as it runs the build process in the same terminal window without proper highlighting. I therefore usually run the make command in another window on my second monitor so that I have both proper highlighting and can look at the build errors the compiler shows me in one window while scrolling through the source code in my main vim window. This is also quite tedious because it requires me to change focus to another window, then type in the make command.

Now, my question is as follows: Is it possible to make vim run the make command in this other window without having to change focus? This way, I could just map the "build in other window" command to some key in vim and could achieve all of this with a single key press.

My system is Manjaro Linux with i3 as DWM.

(I was unsure wether to post this on stackoverflow or here, please forgive me if this is the wrong forum.)

1

With named pipes:

  1. Open your source code with Vim in one terminal.
  2. Open a second terminal where you want the make output and create a new pipe with mkfifo mypipe.
  3. In the second terminal, run watch --color -n 0.3 'cat mypipe' to always be reading the named pipe.
  4. In the first terminal, run :!script -q -c "make" mypipe > /dev/null in Vim after saving changes to your source code.

We have to use script to trick the program to keeping the colored output. Make sure your version of watch supports color.

1

You may add this to your ~/.vimrc:

nnoremap <Space> :vert :term make<CR><C-W><C-w>

Here we remap the Space key :vert :term make<CR><C-W><C-w> command, which means:

  • :vert Make a vertical split;
  • :term Run a terminal in the new window;
  • make<CR> Run make in that terminal;
  • <C-W><C-w> Put focus back in the original window.

I hope this is what you are after. Of course, choose the key/combination you want (which does not need to be Space) and if you prefer a horizontal split, just remove the :vert bit.

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.