2

I use nvim as my main editor, and I run services using systemd. When I edit a unit file directly, there is a built in filetype for systemd, which is great.

However, when I edit a service file with

systemctl edit my-service.service

systemd copies the file to a temporary file named something like .#my-service.servicee18f4d3ef193cd58 and opens it as a buffer, and the filetype isn't detected because of the filename, so I have to manually set it with set ft=systemd in nvim.

I read about the SYSTEMD_EDITOR environment variable, and tried setting it in my zshrc like so:

export SYSTEMD_EDITOR='nvim -c "set ft=systemd"'

but that results in nvim being launched with two buffers named ft=systemd" and .#my-service.servicee18f4d3ef193cd58 when I issue systemctl edit --full my-service.service

How can I correctly set that environment variable? or, alternatively, how can I expand vim's ft detection to include files of this naming pattern?

jameh
  • 211
  • 1
  • 7

1 Answers1

2

I realized I do know how to set a filetype based on extension in my vimrc:

autocmd BufNewFile,BufRead *.service* set ft=systemd

I'm still interested in what my quoting error in the question is.

jameh
  • 211
  • 1
  • 7
  • 3
    See also Vim's [`runtime/filetype.vim`](https://github.com/vim/vim/blob/2c7f8c574f1f8723d59adca3fec8fb89c41cf8c9/runtime/filetype.vim#L1651). The quoting error is simply that `systemd` isn't running `sh -c "$SYSTEMD_EDITOR"`. It's [splitting the variable on whitespace](https://github.com/systemd/systemd/blob/2d69cf6eb09adb3dca7937aef90488872ff36cd2/src/systemctl/systemctl.c#L7645). So quoting is ineffective. In general you can't rely on quoting, or even whitespace splitting, on the various `EDITOR` variables. – muru Apr 27 '20 at 08:25
  • @muru Yes, this! Please post it as an answer. – filbranden Apr 27 '20 at 12:23
  • @muru thanks for that link! I put in an issue report [here](https://github.com/vim/vim/issues/6003) I think `nvim` merges those changes downstream – jameh Apr 27 '20 at 19:16