Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active March 2, 2024 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Konfekt/5748fbe2d72749ab9b2ea8362f703a7e to your computer and use it in GitHub Desktop.
Save Konfekt/5748fbe2d72749ab9b2ea8362f703a7e to your computer and use it in GitHub Desktop.
Vim commands to pipe text to aichat and open a chat window
if executable('aichat')
command! -range -nargs=+ -complete=customlist,s:AIChatRoleCompletion AIRole <line1>,<line2>call s:AIRole(<q-args>)
function! s:AIRole(instruction) range
let instruction = trim(a:instruction)
let i = match(instruction . ' ', '\s')
let role = instruction[0:i-1]
let instruction = trim(instruction[i:-1])
let prompt = !empty(instruction) ? shellescape('Please consider: ' . instruction . '.') : ''
" term ++hidden ++open
exe a:firstline.','.a:lastline . 'term aichat --role ' . role . ' ' . prompt
endfunction
command! -range=0 -nargs=+ -complete=customlist,s:AIChatRoleCompletion AIEditRole <line1>,<line2>call s:AIEditRole(<count>, <q-args>)
function! s:AIEditRole(uses_range, instruction) range
let instruction = trim(a:instruction)
let i = match(instruction . ' ', '\s')
let role = instruction[0:i-1]
let instruction = trim(instruction[i:-1])
let prompt = !empty(instruction) ? shellescape('Please consider: ' . instruction . '.') : ''
if a:uses_range
exe a:firstline.','.a:lastline . '!aichat --role ' . role . ' ' . prompt
else
exe 'read! aichat --role ' . role . ' ' . prompt . 'Your input is: ' . getline(line('.')))
endif
endfunction
command! -range=0 -nargs=1 -complete=customlist,s:AIChatRoleCompletion AIChatRole call s:AIChatRole(<count>, <f-args>)
function! s:AIChatRole(uses_range, role)
let role = a:role
if a:uses_range
exe 'tabnew | tab ' . a:firstline.','.a:lastline . 'term ++curwin ++rows=' . &lines . ' ' . 'aichat --role ' . role
else
exe 'tabnew | tab term ++curwin ++rows=' . &lines . ' ' . 'aichat --role ' . role
else
endfunction
function! s:AIChatRoleCompletion(A,L,P) abort
let l:targets = systemlist('aichat --list-roles')
return filter(l:targets, 'v:val =~ "^' . a:A . '"')
endfunction
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment