0

I am trying to write a plugin and I observed that I have identical ftplugin files. e.g. ftplugin/python.vim and ftplugin/julia.vim have exactly the same content and it looks like the following:

vim9script 

import autoload "../myfunctions.vim"

b:some_var = g:plugins_some_var[&filetype]

augroup my_group
    autocmd! * <buffer>
    autocmd BufEnter <buffer> myfunctions.MyFunc()
augroup END

How can I re-arrange my code structure to avoid such repetitions?

2 Answers 2

1

May be you could move that code into a plugin file and in the autocommand, you test for the existence of &ft key in your g:plugins_some_var global variable. If so, you call the function.

5
  • I tried to add b:some_var = get(g:plugins_some_var, &filetype, "") to plugin/myplugin.vim but either if I open a python file or exactly myplugin.vim and run :echo b:some_var I get E121: Undefined variable: b:some_var. I think - but I may be wrong - that the mechanism won't work because myplugin.vim is sourced only once. Hence, if I change file b:some_var = get(g:plugins_some_var, &filetype, "") won't be executed.
    – Barzi2001
    Commented Apr 28, 2023 at 17:03
  • 1
    Never define buffer variables in global scope of plugin files. In functions called from buffer related events, only then it could make sense (or in the global scope of ftplugin files as they are sourced on buffer related events) Commented Apr 28, 2023 at 17:09
  • Exactly. It would be weird to have b: variables in plugin files. In-fact it didn't work.
    – Barzi2001
    Commented Apr 28, 2023 at 17:13
  • I usually simply put my functions in autoload plugins. And if I need ft specialized functions, I would have autoload/{myinitials}/{filetype}/{pluginname}.vim (with vimscript 1 at least. I've never tried the new scripting language from vim 9) Commented Apr 28, 2023 at 19:17
  • I never used the former vimscript instead, I started like two months ago and I decided to start directly with the new. It doesn’t look much different though. Regarding the autoload mechanism you can place your files wherever you want and use import autoload, i.e. you don’t need to place stuff necessarily to’ autoload folder in vim9script.
    – Barzi2001
    Commented Apr 28, 2023 at 21:56
0

Just use symlinks. This is the easiest possible solution.

0

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.