Skip to content
Enno edited this page Mar 15, 2024 · 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:

Aliases are a simple and effective way to run :AI specific prompts (or, using the -range option, also :AIEdit).

Suggest a git commit message

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:config, l:prompt)
endfunction
command! GitCommitMessage call GitCommitMessageFn()

Add programming language to the prompt

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
  let l:instruction = "programming language is " . &filetype
  if a:0
    let l:instruction = l:instruction . " - " . a:1
  endif
  exe a:firstline . ',' . a:lastline . 'call vim_ai#AIRun({}, l:instruction)'
endfunction
command! -range -nargs=? AISyntax <line1>,<line2>call AISyntaxFn(<f-args>)