let s:plugin_root = expand(':p:h:h') let s:complete_script_file = s:plugin_root . "/py/complete.py" function! AIRun(...) range let prompt = getline(a:firstline, a:lastline) if a:0 " join arguments and prepend to the prompt let instruction = join(a:000, ", ") . ":" call insert(prompt, instruction, 0) endif let buff_lastline = line('$') let prompt = join(prompt, "\n") echo "Completing..." let output = system("echo " . shellescape(prompt) . " | python3 " . s:complete_script_file . " ") let output = trim(output) execute a:firstline . ',' . a:lastline . 'd' if a:lastline == buff_lastline execute "normal! o" . output . "\" else execute "normal! O" . output . "\" endif endfunction command! -range -nargs=? AI ,call AIRun()