Mussé Redi
Mussé Redi

Reputation: 945

redirecting vim :make output to file

Rather than having vim print the output of the :make command, I want to read the output in a file (which gets updated automatically in vim); so that my compiled file can run right away without having to see the output of the :make command.

I'm using the following makefile

all: compile run

compile: file.cc
       g++ -o file file.cc

run: file
       ./file

How does one redirect the output of the :make command in a way that it isn't also printed to the screen by vim?

Upvotes: 1

Views: 1815

Answers (3)

Peter Rincker
Peter Rincker

Reputation: 45107

Use :silent to remove the output and "press enter" prompt. I suggest a nice mapping or command:

command! -nargs=* Smake silent make <args>
nnoremap <f5> :silent make<cr>

:make will populate the quickfix list with the results from :make. Use :copen to open the quickfix window.

For more help see:

:h :command
:h silent
:h :make
:h 'makeprg'
:h quickfix

Upvotes: 0

You can execute this command:

:silent exec "!make >Output" | :redraw!

The file Output contains the last output of the executed make command.

Upvotes: 1

grochmal
grochmal

Reputation: 3027

First of all we have https://vi.stackexchange.com/ , you can get better answers about Vim in there.


Second, I'll argue that a Makefile is no place to run a program, the idea behind make is to catch compilation errors. But assuming you have your reasons (e.g. ./file opens a graphical display) there are a couple of ways to perform this in Vim:

For a start you can set makeprg to perform the redirection:

:set makeprg=make\ >/dev/null\ 2>&1

(You can change /dev/null to an actual file)

But that still leaves the line:

Press ENTER or type command to continue

And asks for confirmation, which may be annoying when you know that there is no output.


To get rid of the confirmation line you can use silent as follows:

set makeprg=make\ >/dev/null\ 2>&1
function! MyMake()
  silent make
  redraw!
endfunction
command Mm call MyMake()

And now you can do:

:Mm

To perform the make and go back to straight to Vim. (the redraw! is needed only in some terminals)

Upvotes: 1

Related Questions