0

I often see #-namespaces in plugins source code, e.g coc#status(), fzf#run(), fugitive#extract_git_dir. I would like to use namespaces in my own config, mappings and commands source code also. But I'm getting errors E746: Function name does not match script file name so it turns out that namespace must match the name of the file where the function is defined. But file names may have hyphen while function names can't. So how can I define function with namespace in a file foo-bar.vim? Is it possible?

1
  • 4
    That # is not a namespace as in other general purposed programming langs. It indicates function is autoloaded.
    – Maxim Kim
    Jun 15 at 10:28
5

So how can I define function with namespace in a file foo-bar.vim? Is it possible?

I don't think it is possible.

See :h 41.15 of the User Manual for autoload feature of vim (which you call namespace)

When such a function is called, and it is not defined yet, Vim will search the
"autoload" directories in 'runtimepath' for a script file called
"filename.vim".  For example "~/.vim/autoload/filename.vim".  That file should
then define the function like this: >

    function filename#funcname()
       echo "Done!"
    endfunction

The file name and the name used before the # in the function must match
exactly, and the defined function must have the name exactly as it will be
called.

Fact 1:

The file name and the name used before the # in the function must match exactly

Fact 2:

Minus - is a binary operation in vimscript -- test-me#hello() is considered as test variable minus me variable.

Resolution

Do not use dashes in the names of your autoloaded files.

1
  • There's an exception in that a namespace#... function can be defined in a plugin/ file (not only in an autoload/ one), but even then, I believe the file name must match namespace.vim. An example is if which defines some functions here. Note that this is pretty confusing, since one who's debugging it and looking for these functions will go to autoload/ and be befuddled by them not being there... So it's not a practice I would recommend anyways.
    – filbranden
    Jun 15 at 14:00

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.