0

I would like to make a plugin for Neovim that does some pretty expensive processing for a text in a file. I would need to keep processed data as well as a hash signature of a file (to avoid processing of the data without need).

Is there a convention where to keep such information generated by a plugin?

2
  • Probably tempname()? Or (*nix) use a hidden directory like ~/.myplugin or /path/to/script/.mycache or something; ~/.cache/myplugin also…
    – D. Ben Knoble
    Aug 22, 2021 at 18:52
  • 2
    :help stdpath()
    – Matt
    Aug 22, 2021 at 19:00

1 Answer 1

3

There is no vim convention for this per se, but there are various operating system conventions.

On neovim, your best bet is probably stdpath('cache'). On linux, this usually resolves to ~/.cache/nvim. If needed, you can create a subdir for your plugin like

let l:dir = stdpath('cache') . '/myplugin'
if !isdirectory(l:dir)
    call mkdir(l:dir, 'p')
endif

In vim, you can emulate stdpath (for linux at least) using

let l:stdpath_cache = (empty($XDG_CACHE_HOME)
    \ ? $HOME . '/.cache'
    \ : $XDG_CACHE_HOME) . '/vim'

Some plugins write to their install directory but this is not a good practice, as plugins can be installed to system directories to which the user does not have access.

7
  • RE ~/.cache, agreed. For Windows perhaps $USERPROFILE\AppData\Local\cache ... though I'm less sure of that one. (The dir does exist on my main Win box, FWIW.)
    – B Layer
    Aug 22, 2021 at 19:18
  • I see that stdpath("cache") on windows points to $USERPROFILE\AppData\Local\Temp\nvim but using that Windows temp dir is like using /tmp on *nix...and not a great choice here, IMO.
    – B Layer
    Aug 22, 2021 at 19:24
  • @BLayer generally a bad idea to hard code paths, which is one point of stdpath(), I guess the problem then is neovim's choice of paths on windows is inappropriate.
    – Mass
    Aug 22, 2021 at 20:05
  • On Windows $USERPROFILE\AppData\Local always exists. It's a standard path meant for, as it says, application data and creating a subdirectory for your particular application is SOP. Though I agree in principle to what you're saying I wouldn't consider this a hard coded path (plus it uses an env var which means it is not hardcoded). (The "cache" part can be dropped if it's not guaranteed to be there...which I haven't googled. Also, I forgot the "{myplugin}" part in my original comment.)
    – B Layer
    Aug 22, 2021 at 23:38
  • Not sure but if you mean something else by "hard coded", i.e. any path you create that doesn't come from stdpath() I'd argue that what it returns is just as hardcoded. :) Well, except for the platform specific part. That was implied in everything I said, i.e. the plugin would use if has('win32') or equivalent when choosing the system-created dir in which to create an app data subdir.
    – B Layer
    Aug 22, 2021 at 23:59

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.