9

I'm writing my first vim plugin (viml + python). One command that the plugin has is "GetStepCommand()" and it basically fetches data from a remote data source, I massage the data a bit and copies it into the buffer so the user can start editing it. There is a parameter that the user has to supply to "GetStepsCommand" and that is the search path to where the data resides, for ex: /projects/procedure/step

Now that path can be long and its easy to miss-spell something. So I wanted to implement my own tab-completion for the parameter part. Vim already takes care of auto-completing the command by tabbing, but of course it can not have any knowledge about how to complete the parameter (something I'll solve myself).

But first I need to know: - if/how I can intercept the keypress in command-mode - fetch/get the command-line that the user currently is writing - test if it's in command-mode or insert/view-mode - and finally return an updated command-line (tab-completed) that the user can continue writing on in ':' after the keypress.

Any pointers, tips, articles, tutorials... i.e information is greatly appreciated

1 Answers1

11

When the argument to your custom command is a file-system path, it's simply a matter of adding -complete=file to your :command definition, like this:

:command -nargs=1 -complete=file MyCommand echomsg <q-args>

You don't need to intercept keypresses in command-line mode (and you should not, for this would lead to bad interactions with other plugins!) Vim offers other default completions (cp. :help :command-complete), even a custom one where a Vimscript function is invoked to determine the completion candidates.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks for the tips, I'll look through them. However the argument isn't a file-system path, its data (describing a path in the DB) from the system where I'm pulling the data into the buffer. So that's why I will need to do some of my own processing of the auto-completion. – Kristoffer Nordström Sep 28 '12 at 10:53
  • Thank you for the reference to ":command -complete" ... just finished playing around with it... should be no problem from here. – Kristoffer Nordström Sep 28 '12 at 11:25
  • 3
    @KristofferNordström One note about this particular completion type: `-complete=file` is not a completion option besides its name. It will make completion work to complete files, **will expand environment variables, will prevent you from typing unescaped space** (normally with `-nargs=1` and `MyCommand a b` argument will be `a b`), **will expand patterns and perform backtick expansion** (echoing an error if these expansions result in more filenames than `-nargs` allows). And none of these things are mentioned in help. – ZyX Sep 28 '12 at 13:06
  • Is there a way to specify a path to load the files from? (so not the current directory) – math2001 Aug 24 '17 at 09:34