3

I'd like to run make and display the output when I press Ctrl+m in Vim. I know how to run make and display the output while in command mode, thanks to this StackOverflow answer, namely with:

:make | copen

but how do I bind this to a keyboard shortcut? I managed this when I was just running make and not displaying the output namely with:

noremap <C-m> :make<CR>

in my ~/.vimrc file. Unfortunately, adding | copen after :make and before <CR> results in this Vim start up error:

Error detected while processing /home/fusion809/.vimrc:
line  157:
E488: Trailing characters:  copen <CR>
Press ENTER or type command to continue
2

When you want to map to a key sequence that contains | you have to be careful. See :help map_bar.

You should probably use this mapping:

noremap <C-m> :make<BAR>copen<CR>
0

<Ctrl-M> is equivalent to <CR>

First of all, please note that <Ctrl-M> is equivalent to <CR> which you might not use in normal mode and therefore is ok for you. If you use the mapping, you can also press <CR> in normal mode to trigger make.

If you like to use a different mapping, have a look at the plugin

vim-dispatch by Tim Pope

This plugin provides following mappings

  • menter :Make<CR>
  • mspace :Make<Space> (enter arguments)
  • m!+enter :Make!
  • m? Show 'makeprg'

and the new command :Make works as follows

                                                *dispatch-:Make*
:Make [arguments]       Using the current |:compiler| settings, dispatch a
                        build in the foreground.  Adapter strategies vary, but
                        the goal is visibility without stealing focus.  When
                        the build is complete, load the results into the
                        |quickfix| list and call |:cwindow|.  This command is
                        preferred for shorter tasks like "build this file."

:Make! [arguments]      Using the current compiler settings, dispatch a build
                        in the background.  Use |:Copen| to load the results.
                        This command is preferred for longer tasks like "run
                        the entire test suite."

Note the difference between :copen and :cwindow:

                            *:cw* *:cwindow*
:cw[indow] [height] Open the quickfix window when there are recognized
            errors.  If the window is already open and there are
            no recognized errors, close the window.

                        *:cope* *:copen* *w:quickfix_title*
:cope[n] [height]   Open a window to show the current list of errors.

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.