Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim plugin - custom auto-complete of params in command-mode

Tags:

vim

vim-plugin

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

like image 814
Kristoffer Nordström Avatar asked Sep 28 '12 08:09

Kristoffer Nordström


1 Answers

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.

like image 56
Ingo Karkat Avatar answered Sep 20 '22 14:09

Ingo Karkat