It would be fine if after :make command vim opened the new buffer with errors and put the cursor to it. Exactly like :make | copen command. How to map sequence :make | copen?


You could create an abbreviation in the command line mode by adding this to your vimrc:

cabbrev mc make<bar>copen

Now when you'll type mc in the command line it will be replaced by make|copen making it faster to type.

You could also create a mapping to invoke the commands you want:

nnoremap <key> :make<bar>copen<CR>

EDIT As @Sato Katsura pointed it out in the comment my first version of cabbrev is not a good one: Indeed in the first version mc will be replaced no matter where it is placed in the command line, so for example I you want to search in a buffer for the string mc and you try to hit /mc it will be replaced by /make|copen.

To resolve that @Sato linked an interesting tip. With the following syntax mc will be replaced only if it is the first thing typed in the command line:

cabbrev make <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'make<bar>copen' : 'make')<CR>
  • 1
    Thank you! Both spells work well. I put cabbrev make make<bar>copen in .vimrc (I more used to type `make') it works. – Alexey Reshetnyak Aug 13 '16 at 19:31
  • 3
    Here we go again: cabbrev is not that simple. – Sato Katsura Aug 14 '16 at 7:16

The best solution is to not create a mapping at all.

Simply put the following snippet in your vimrc:

augroup quickfix
    " automatic location/quickfix window
    autocmd QuickFixCmdPost [^l]* cwindow
    autocmd QuickFixCmdPost    l* lwindow
augroup END

This will open the quickfix window automatically after :make and other quickfix-related commands like :grep, when there are valid errors (:cwindow versus :copen).

Same for location-related commands like :lvimgrep.

--- EDIT ---

The general structure of an autocommand is:

autocmd [<group>] <EventName>[,<EventName>[,...]] <pattern> <command>

Here, <pattern> is matched against Ex command names:

  • l* matches all command names starting with l (:lgrep, :lmake, etc.),
  • [^l]* matches all command names not starting with l (:grep, :make, etc.).

Quickfix-related command names start with different letters while location-related command names all start with an l so l as first character is a convenient way to discriminate them.

  • This solution doesn't put cursor to the quickfix window, but make | copen does. – Alexey Reshetnyak Aug 13 '16 at 18:57
  • Well, just add | wincmd p after cwindow. – romainl Aug 13 '16 at 19:03
  • | wincmd p spell doesn't works either. It puts cursor to the first error in the window with source code not to quickfix window. – Alexey Reshetnyak Aug 13 '16 at 19:20
  • could you explain, please, where do the patterns [^l]* and l* come from? I mean, what are they for? Or they aren't patterns? – Dmitry Koroliov Jun 3 '17 at 2:16
  • 1
    @user907860, see my edit. – romainl Jun 3 '17 at 8:54

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.