1

If I run the command CoverageShow coverage.py when I have a python file open and a .coverage report in my directory it does a nice job of showing me which lines are included in my testing.

I would like to run this every time I open a file that meets to requirements, so i thought I could put something like

if filereadable(".coverage")
" should somehow check the filetype here too
CoverageShow coverage.py
endif

but when I try to open a file with this in my .vimrc I get E492: Not an editor command: CoverageShow coverage.py which confuses me, because CoverageShow coverage.py certainly works when vim is open, but then I don't really know what I'm doing. Where am I going wrong here?

1 Answer 1

3

The vimrc file is run only once and (I'm pretty sure) before the plugins, since I saw stuff like if g:someConfigVar in some, so the config variables are supposed to be set by the user before the plugin starts to be loaded.

This is the explanation, why your example gives the error E492.

But you don't need this approach, what are you looking for is autocommands. I can't give you an expert advise on this, so you better to read the help yourself :h autocommand, but you can try to use the FileType event, like:

autocommand! FileType python CoverageShow

Or, if you need those checks:

function WrapperFunc()
  if filereadable(".coverage")
    CoverageShow coverage.py
  endif
enffunction
autocommand! FileType call WrapperFunc()

You definitely need autocommands and an appropriate event (either FileType or other).

You better read the docs yourself, since I'm not an expert, and there may be some improvements to the solution. Like putting the autocommand in a special autocommand group, to prevent the excl. mark in autocommand! to remove something it was not intended to remove etc.

2
  • 3
    You should always put your autocommands in augroups, see Why should I use augroup. Also this help topic is worth being listed here :h autocommand-events and I'm wondering if BufReadPost wouldn't be more robust than Filetype since I'm not sure everything needed is there already with Filetype
    – statox
    Commented Jun 17, 2020 at 7:49
  • Thank you for this answer, I will go investigate autocommand, it looks like a very useful tool to know about. And thank you @statox for the links.
    – Clumsy cat
    Commented Jun 17, 2020 at 14:36

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.