I have following line in my .nvimrc

au! BufWritePost *.elm ElmMake

Each time I save an elm file vim tells me that this command does not exist. However I am able to run this command in command more while editing elm file, so there is no doubt in my mind that this command does exist.

I should point out that ElmMake command came with filetype plugin so apparently commands in vimrc are ran in different filetype(vim?)

Which leads me to my question how can I access file type specific commands inside my vimrc?

  • autocommand you showed us should work – Maxim Kim Dec 5 '19 at 3:52
  • Ah! Didn't notice exclamation mark, try remove it au BufWritePost *.elm ElmMake – Maxim Kim Dec 5 '19 at 3:58

I believe the problem you're having is due to the command being registered with -buffer, which means it's only available in buffers of filetype "elm" and for some reason are not available in the autocmd.

Why that is happening, I can't really tell... The event you're using (BufWritePost) is a buffer-specific example and I'd also expect it to run while the buffer is still around... But I might be wrong about that part perhaps...

It's not the case that this will be evaluated in a filetype vim context, the autocmd commands are only really evaluated when called, not when they're defined. So not really sure what's going on there...

I can try to suggest a workaround though.

The definition of the ElmMake command reads:

command -buffer -nargs=? -complete=file ElmMake call elm#Make(<f-args>)

So calling it without any arguments should be the equivalent to:

call elm#Make()

So just call that directly from your autocmd instead. The function definition is global and should work from any context.

For best practice, put it in an augroup block, so it gets properly updated when you reload your vimrc:

augroup vimrc_elm
  au BufWritePost *.elm call elm#Make()
augroup END

Hopefully that will solve the problem!

| improve this answer | |
  • 1
    Thank yo that's very helpful. Seems to have solved it/exposed the error in Make function itself XD – sgp667 Dec 7 '19 at 16:15

Looks like you try to add an autocommand but remove it instead:

3. Removing autocommands                *autocmd-remove*

:au[tocmd]! [group] {event} {pat} [++once] [++nested] {cmd}
            Remove all autocommands associated with {event} and
            {pat}, and add the command {cmd}.

Try it without exclamation mark.

Also, generally it is a good idea to put autocommands to autogroups, like:

augroup ELM_MAKE | autocmd!
    au BufWritePost *.elm ElmMake
augroup end

It creates autocommand group and cleans it up with autocmd! before any autocommand definition. Otherwise each reload of your vimrc (:so $MYVIMRC for example) would create duplicate autocommands.

| improve this answer | |
  • You missed the part that says and add the command {cmd}. So that au! line is somewhat legit, in that it will set that autocmd correctly (replacing any others for the same event/pattern.) So while I agree with your suggestion, it's not really an answer to this question. – filbranden Dec 5 '19 at 17:42
  • @filbranden indeed, I had interpreted documentation in a wrong way. – Maxim Kim Dec 6 '19 at 6:16

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.