3

I'm making a command that runs tests and opens coverage report for the currently opened file.

nnoremap cov :!vendor/phpunit/phpunit/phpunit --coverage-html \
/tmp/coverage/ test/ && google-chrome /tmp/coverage/<CR>

How do I put the contents of filename register (%) into the bash command I want to execute?

nnoremap cov :!vendor/phpunit/phpunit/phpunit --coverage-html \
/tmp/coverage/ test/ && google-chrome /tmp/coverage/FILENAME_GOES_HERE<CR>

Also a closer try:

function CoverageHtml(filename)
    execute '!vendor/phpunit/phpunit/phpunit' '--coverage-html' '/tmp/coverage/' 'test/'
    let f = "/tmp/coverage" + a:filename
    execute '!google-chrome' f
endfunction

function CoverageHtmlInit()
    "fails in the next line. 
    let fn = :echo @%
    return CoverageHtml(fn)
endfunction

nnoremap cov :call CoverageHtmlInit()
0

2 Answers 2

2

To refer to the current file, you can use %.

You can even use modified versions of the path, by applying one ore more of the modifiers as documented in :help %:. To highlight just a few interesting ones from the help page:

:p  Make file name a full path.  Must be the first modifier.  Also
    changes "~/" (and "~user/" for Unix and VMS) to the path for
    the home directory.  If the name is a directory a path
    separator is added at the end.  For a file name that does not
    exist and does not have an absolute path the result is
    unpredictable.  On MS-Windows an 8.3 filename is expanded to
    the long name.

:~  Reduce file name to be relative to the home directory, if
    possible.  File name is unmodified if it is not below the home
    directory.

:t  Tail of the file name (last component of the name).  Must
    precede any :r or :e.

:s?pat?sub?
    Substitute the first occurrence of "pat" with "sub".  This
    works like the |:s| command.  "pat" is a regular expression.
    Any character can be used for '?', but it must not occur in
    "pat" or "sub".
    After this, the previous modifiers can be used again.  For
    example ":p", to make a full path after the substitution.
0

This was the partial solution based on janos' answer. Can't manipulate the %, so needed another test directory structure to make it work.

function CoverageHtml(filename)
    execute '!vendor/phpunit/phpunit/phpunit' '--coverage-html' '/tmp/coverage/src/' 'test/'
    execute '!google-chrome' '/tmp/coverage/%.html'
endfunction

nnoremap cov :let a=@% <CR>:call CoverageHtml(a)<CR>
3
  • I don't understand what you mean by "Can't manipulate the %". Any chance you could clarify?
    – Rich
    May 11, 2018 at 12:41
  • From that context I can't save % to a variable to remove the preceeding src/ from the path. May 11, 2018 at 13:01
  • I think that you should be able to, but I also think you shouldn't need to save anything into a variable to remove the src from the path. Doesn't replacing the execute line in your function with the following work? !google-chrome /tmp/coverage/%:t.html This uses the :t mentioned by @janos to remove the path.
    – Rich
    May 11, 2018 at 13:11

Your Answer

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

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