1

I'm writing a custom ftplugin for handling systemd file type. I've copied parts of code from vim's own systemd ftplugin and now the latter conflicts with my own code:

Error detected while processing BufRead Autocommands for "*/etc/systemd/*.conf"..FileType Autocommands for "*"..function <SNR>1
4_LoadFTPlugin[18]..script /usr/share/vim/vim90/ftplugin/systemd.vim:
line   12:
E174: Command already exists: add ! to replace it: Sman silent exe '!' . KeywordLookup_systemd(<q-args>) | redraw!

From what I see, the error comes from the system-wide ftplugin. The problem is, it uses the normal b:did_ftplugin guard improperly. Instead of this:

if exists("b:did_ftplugin")
  finish
endif
let b:did_ftplugin = 1

...it does this:

if !exists('b:did_ftplugin')
  " some code that sets b:did_ftplugin
endif

" some more code

As such, the code that is not guarded comes into conflict with my own code.


I suppose this is something that should be patched upstream, but in the meantime, is there a way to prevent Vim from loading the system-wide ftplugin/systemd.vim and only load mine?

4
  • I suspect the oddness is to support :runtime ftplugin/systemd.vim, like for the Man plugin, but I haven’t checked.
    – D. Ben Knoble
    Commented Oct 3, 2023 at 21:28
  • @D.BenKnoble My apologies, I'm fairly new to Vimscript. Could you please explain what exactly is this supposed to achieve?
    – intelfx
    Commented Oct 3, 2023 at 21:53
  • I just looked at it, and I was wrong. (See ftplugin/man.vim for some serious weirdness.) I'll contact the maintainer and suggest a patch.
    – D. Ben Knoble
    Commented Oct 4, 2023 at 15:25
  • github.com/vim/vim/issues/13357
    – D. Ben Knoble
    Commented Oct 17, 2023 at 16:23

2 Answers 2

2

Introduce systemd2

What you could do is selecting another filetype for systemd (e.g. systemd2) by having a ftdetect/systemd2.vim that set it.

~/vimfiles/ftdetect/systemd2.vim

" Systemd unit files
au BufNewFile,BufRead */systemd/*.{automount,dnssd,link,mount,netdev,network,nspawn,path,service,slice,socket,swap,target,timer}    setf systemd2
" Systemd overrides
au BufNewFile,BufRead */etc/systemd/*.conf.d/*.conf setf systemd2
au BufNewFile,BufRead */etc/systemd/system/*.d/*.conf   setf systemd2
au BufNewFile,BufRead */.config/systemd/user/*.d/*.conf setf systemd2
" Systemd temp files
au BufNewFile,BufRead */etc/systemd/system/*.d/.#*  setf systemd2
au BufNewFile,BufRead */etc/systemd/system/.#*      setf systemd2
au BufNewFile,BufRead */.config/systemd/user/*.d/.#*    setf systemd2
au BufNewFile,BufRead */.config/systemd/user/.#*    setf systemd2

The ftplugin/systemd.vim and friends ($VIMRUNTIME/indent/systemd.vim, $VIMRUNTIME/syntax/systemd.vim, $VIMRUNTIME/compiler/systemd.vim) will not be executed anymore and you can put your code in ~/vimfiles/ftplugin/systemd2.vim and friends.

Amend the standard solution

Another solution is to amend the systemd standard solution by moving part your code in ~/vimfiles/after/ftplugin/systemd.vim and make use of the command! to override the Sman command if you need to change it.

2
  • 1
    Thank you! It did not even cross my mind that I could just introduce another filetype :-)
    – intelfx
    Commented Oct 4, 2023 at 14:51
  • 1
    +1 for overriding in ~/.vim/after/ftplugin/systemd.vim Commented Oct 4, 2023 at 14:56
0

This is fixed in a recent runtime commit. If you can run recent versions, you should be able to get the upstream change and continue as usual.

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.