3

Writing a vim plugin, I can use

function! s:InternalMethod()
endfunction

for InternalMethod to be local to the plugin environment. It will not be directly available to users nor overwrite their methods.

How do I perform a similar encapsulation on internal commands?

command! -nargs=1 InternalCommand <q-args>

Does this even make sense?

2
  • 1
    Well, does it? :) I think you can ask the same thing about mappings then.
    – VanLaser
    Feb 20 '16 at 13:08
  • @VanLaser Yeah, I guess you're right. I finally came out with another InternalMethod.. which finally seems like a pretty sane approach.
    – iago-lito
    Feb 20 '16 at 13:51
4

Vim's help about defining a new command (:com[mand][!] [{attr}...] {cmd} {rep} at :h E174) states:

Define a user command. The name of the command is {cmd} and its replacement text is {rep}. The command's attributes (see below) are {attr}. If the command already exists, an error is reported, unless a ! is specified, in which case the command is redefined.

Note the user attribute - in my understanding, commands (and mappings) are created for the user; this means that indeed it won't make much sense to have commands "not directly available to users".

In other words, since one can always call a function from a script, commands and mappings are user commodities :)

1
  • 1
    Which makes perfect sense indeed ;)
    – iago-lito
    Feb 20 '16 at 13:56
2

Yes it makes sense. I have a few plugins that define command not exported to the end user. Sometimes, we, plugin writers, are the user. And commands can help us provide more advanced stuff.

The way I do it: I define the commands I need in the function scope where I use them, and finally (in a finally clause), I delete the commands I've defined for my own selfish needs.

3
  • Well, why not? :) But how do you actually define them "in the function scope where you use them"? i.e. is there any actual difference in how the command is defined? (similar to s:function() for functions).
    – VanLaser
    Feb 20 '16 at 22:33
  • I suppose, if you source the file manually, you get all those commands defined, but if the user loads the plugin, the commands get deleted automatically - that's nice :) (upvoted)
    – VanLaser
    Feb 20 '16 at 22:40
  • Everything is as usual. I just don't forget to call delcommand when I'm done. Check for instance the s:help() function over there Feb 20 '16 at 23:23

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.