Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
Well, does it? :) I think you can ask the same thing about mappings then. – VanLaser Feb 20 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 at 13:51
up vote 4 down vote accepted

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 :)

share|improve this answer
1  
Which makes perfect sense indeed ;) – Iago-lito Feb 20 at 13:56

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.

share|improve this answer
    
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 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 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 – Luc Hermitte Feb 20 at 23:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.