3

For current source file with the following name pattern: xxx_test.cpp how do you execute make file with the following patter xxx.makefile. I tried using

set makeprg=make\ -f\ fnameescape(substitute(expand("%"),"_test.cpp$",".makefile","")))

but it gives me the following output:

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `make -f fnameescape(substitute(expand(  2>&1| tee /tmp/user/1000/v98gzpR/261'

I am new to VIM, I tried to parse the vim help files, but cannot find an answer on how to have VIM execute the substitution.

EDIT: The right answer is presented by Sato Katsura here: Why is make running against a wrong file?

Thanks...

  • I see a trailing ) in your setting, is that related to the issue ? I also have the following error : zsh:1: bad pattern: fnameescape(substitute(expand( 2 – nobe4 Aug 24 '15 at 16:13
  • "set" doesn't evaluate the right-hand side of the = as vimscript, it just treats it as a string; your shell is not going to understand what "fnameescape(...)" is. Using "let" with string concatenation as in Sato's answer should work though, as it will evalute the right-hand side as vimscript. – user72 Aug 24 '15 at 17:56
4

The trick is to forget about wrapping your mind around set syntax, and use let instead:

let &makeprg = 'make -f ' . fnameescape(substitute(expand('%'), '\m_test\.cpp$', '.makefile', ''))

See :help :let-& for details.

  • Works great, but is there a way to prevent vi from going to console, just let the list of errors window display the errors? – user1135541 Aug 24 '15 at 18:05
  • @user1135541 Not reliably. You can however run system() followed by cgetexpr instead of make. – Sato Katsura Aug 24 '15 at 18:35
  • Strange, all of a sudden, I am getting this error [No write since last change] /home/sporty/.vimrc:1: *** missing separator (did you mean TAB instead of 8 spaces?). Stop. Press ENTER or type command to continue – user1135541 Aug 24 '15 at 18:59
  • You seem to be running make against your vimrc. Perhaps do the above dance with makeprg only for the relevant files? – Sato Katsura Aug 24 '15 at 19:10
  • Hmm, so I am in VIM, the current file is the ring_buffer_test.cpp. Should the VIM not replace % with ring_buffer_test.cpp and do the substitution? Why is it running against some random file? In fact it did run it against some other source file. – user1135541 Aug 25 '15 at 13:22

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.