34

I use vim for coding. When I have to compile the current file, Currently I use :!g++ % && ./a.out or :make. The errors/output displayed are gone when I press enter and get back to the file. I wish the errors and output are displayed in a vertical split by the side. It would be nice if output and error streams are in separate buffers. How can this be done? Errors and Output buffer(s) should be updated when I compile again and it should not create new buffers. How can do this? some vim pluggin/function? or a oneliner :P?

balki
  • 26,394
  • 30
  • 105
  • 151

2 Answers2

52

oneliner:

:make | copen

See http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix-window

David Winslow
  • 8,472
  • 1
  • 31
  • 27
  • wow. great! Can I have a vertical split instead of a horizontal split? – balki Jul 01 '11 at 15:34
  • 2
    @balki, try putting a `vert` just before the `copen`. – Karl Bielefeldt Jul 01 '11 at 15:37
  • 2
    Yep, just use ``vert copen`` instead of ``copen``. See http://vimdoc.sourceforge.net/htmldoc/windows.html#opening-window for more info. – David Winslow Jul 01 '11 at 15:38
  • Thanks for vertical split. How do I use this quickfix window when compiling like this `:!g++ %` ? – balki Jul 06 '11 at 15:54
  • 1
    Don't compile like that :) Instead, set `makeprg` and `errorformat` so the `:make` command will run g++ for you. I'm not familiar with it but vim comes with a compiler configuration for gcc you might be able to start from. See http://vimdoc.sourceforge.net/htmldoc/quickfix.html#compiler-select – David Winslow Jul 06 '11 at 16:44
  • Jump between windows with Ctrl-w j (down) and Ctrl-w k (up), etc. If you're new to vim, you'll learn these quick. – Sk606 Nov 20 '12 at 20:46
-1

If you want compile and run if compile succeeded. (i.e !g++ % && ./a.out )

Create a shells script with the following line,

g++ $1 -o /tmp/a.out && /tmp/a.out

Now set the makeprg like this. set makeprg=compileNrun.sh\ %

Cannot set the whole command directly as makeprg because of &&. If set in makeprg directly, the above command will be expanded to,

!g++ file.C -o /tmp/a.out && /tmp/a.out 2>&1 | tee /tmp/errorFile

Hence compilation errors wont be redirected to the error file if compilation failed ;P as && takes precedence over |

balki
  • 26,394
  • 30
  • 105
  • 151