-
Notifications
You must be signed in to change notification settings - Fork 66
Custom Commands
Martin Bielik edited this page Apr 23, 2023
·
8 revisions
This is a collection of custom commands that can be implemented using exposed vim-ai functions. To use it, simply add it to your .vimrc
or init.vim
file.
Related documentation:
Usage: Run :GitCommitMessage
Tip: Run :AIR
or hit your keyboard shortcut to redo AI command and get an alternative suggestion.
" custom command suggesting git commit message, takes no arguments
function! GitCommitMessageFn()
let l:diff = system('git --no-pager diff --staged')
let l:prompt = "generate a short commit message from the diff below:\n" . l:diff
let l:range = 0
let l:config = {
\ "engine": "chat",
\ "options": {
\ "model": "gpt-3.5-turbo",
\ "initial_prompt": ">>> system\nyou are a code assistant",
\ "temperature": 1,
\ },
\}
call vim_ai#AIRun(l:range, l:config, l:prompt)
endfunction
command! GitCommitMessage call GitCommitMessageFn()
Usage: Run <selection>?:AIS {instruction}?
In code prompts, it is useful to put information about the programming language, for example :AI generate a hello world method in Java
. This custom command, reads this information from the file type and prepends it to the instruction automatically. Therefore, the usage might be more convenient: :AIS generate a hello world method
.
" custom command adding filetype to the instruction
function! AISyntaxFn(range, ...) range
let l:instruction = "programming language is " . &filetype
if a:0
let l:instruction = l:instruction . " - " . a:1
endif
if a:range
'<,'>call vim_ai#AIRun(a:range, {}, l:instruction)
else
call vim_ai#AIRun(a:range, {}, l:instruction)
endif
endfunction
command! -range -nargs=? AISyntax <line1>,<line2>call AISyntaxFn(<range>, <f-args>)