vimtricks.wiki Concise Vim tricks, one at a time.

How do I automatically open or close the quickfix window based on results?

Answer

:cwindow

Explanation

The :cwindow command intelligently manages the quickfix window — it opens the window only if there are entries in the quickfix list, and closes it if the list is empty. This is the smart alternative to :copen.

How it works

:cwindow    " Open quickfix window if entries exist, close if empty
:copen      " Always open quickfix window (even if empty)
:cclose     " Always close quickfix window
:lwindow    " Same as :cwindow but for the location list

Practical usage in autocmds

" Auto-open quickfix after :make
autocmd QuickFixCmdPost [^l]* cwindow
autocmd QuickFixCmdPost l*    lwindow

" Auto-open after :grep
autocmd QuickFixCmdPost grep cwindow

With this autocmd, running :make or :grep will automatically show the quickfix window only when there are errors or results.

Navigating the quickfix list

:cnext      " Next entry
:cprev      " Previous entry
:cfirst     " First entry
:clast      " Last entry
:cc 5       " Go to entry 5
:colder     " Go to previous quickfix list
:cnewer     " Go to newer quickfix list

Tips

  • :cwindow is preferred over :copen in scripts and autocmds because it doesn't create an annoying empty window
  • Add a height parameter: :cwindow 10 sets the window to 10 lines
  • The quickfix window is a regular buffer — you can search, yank, and navigate within it
  • Press <CR> on any line in the quickfix window to jump to that location
  • Vim remembers up to 10 quickfix lists — use :colder/:cnewer to browse history

Next

How do I always access my last yanked text regardless of deletes?