3

Here is my code:

autocmd BufWritePost *.c,*.cpp,*.h
  \   let dir=expand('<amatch>:p:h')
  \ | if filereadable(dir.'/Makefile') || filereadable(dir.'/makefile')
  \ |   Make > /dev/null
  \ | endif

I want vim to execute the command Make >> /dev/null if there is a Makefile or makefile in the same directory as the current file. However, the command executed is

:Dispatch make > /dev/null | endif

That is to say, | endif is treated as part of the command. How can I tell vim it's part of an if-statement?

The command Make and Dispatch come from vim-dispatch, if it's relevant.

1 Answer 1

3

If the :Make command you are using is this one, then your issue may come from the fact that it was not given the -bar attribute when it was defined. See :h :command-bar:

-bar        The command can be followed by a "|" and another command.
            A "|" inside the command argument is not allowed then.
            Also checks for a " to start a comment.

There are other solutions, but the simplest one may be to hide | endif from :Make by wrapping it inside a string, and executing the latter with :execute:

autocmd BufWritePost *.c,*.cpp,*.h
  \   let dir=expand('<amatch>:p:h')
  \ | if filereadable(dir.'/Makefile') || filereadable(dir.'/makefile')
  \ |   exe 'Make > /dev/null'
  \ | endif

For the complete list of commands which, like :Make, see the | as part of their argument, see :h :bar:

These commands see the '|' as their argument, and can therefore not be
followed by another Vim command:
    :argdo
    :autocmd
    :bufdo
    :cdo
    :cfdo
    :command
    :cscope
    :debug
    :folddoopen
    :folddoclosed
    :function
    :global
    :help
    :helpfind
    :lcscope
    :ldo
    :lfdo
    :make
    :normal
    :perl
    :perldo
    :promptfind
    :promptrepl
    :pyfile
    :python
    :registers
    :read !
    :scscope
    :sign
    :tcl
    :tcldo
    :tclfile
    :vglobal
    :windo
    :write !
    :[range]!
    a user defined command without the "-bar" argument |:command|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.