Search code examples
vimtab-completion

Vim: Custom tab complete that retrieves results from a bash script


I have written a bash script finds rhymes for any word. I would like to be able to tab complete the results of this script.

I imagine this is possible with vimscript. The basic steps are look at the line above the current line, pass that line as an argument, and then show the tab completion view (possibly via some plugin) and show the result returned from the script.

I've written some vim script before, but I don't quite know where to start with executing a bash script or opening the tab completion menu. Does anyone know how this might be possible?


Solution

  • You can use fzf and fzf.vim for this. Here's a sample key mapping.

    inoremap <expr> <c-x><c-k> fzf#complete('./script.sh ' . getline(line(".")-1))
    

    fzf#complete can run the script and open a menu with the output of that script. You can fuzzy search the output or use arrow keys to select an entry. Selected entry gets inserted in the vim buffer.

    getline(line(".")-1) passes the line above current line to the script called script.sh.

    This is not exactly tab completion like you asked, but it's serving the same purpose and also has fuzzy search through results from your script.