Skip to content

Commit

Permalink
feat: implemented gptme.vim plugin (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare authored Nov 1, 2024
1 parent cdd8c6b commit a04ee05
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ You can find more [Demos][docs-demos] and [Examples][docs-examples] in the [docu
- 📜 RAG to automatically include context from local files (see [#59](https://github.com/ErikBjare/gptme/issues/59))
- 💻 "Computer use" as hyped by [Anthropic][anthropic-computer-use] (see [#216](https://github.com/ErikBjare/gptme/issues/216))
- 🌐 Web UI frontend (see docs for [server][docs-server])
- 📝 vim plugin (see [...])


### 🛠 Use Cases

Expand Down
66 changes: 66 additions & 0 deletions scripts/vim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# gptme Vim Plugin

A Vim plugin for [gptme](https://gptme.org) integration, allowing you to interact with gptme directly from your editor.

## Features

- Run gptme queries with context from your current buffer
- Automatically includes surrounding lines as context
- Results shown in a new buffer
- Configurable context size and key mappings

## Installation

### Using a Plugin Manager

#### [vim-plug](https://github.com/junegunn/vim-plug)

Add this to your `.vimrc`:

Plug 'ErikBjare/gptme', { 'rtp': 'scripts/vim' }

Then run:

:PlugInstall

### Manual Installation

Copy the contents of this directory to your ~/.vim directory:

cp -r plugin doc ~/.vim/

## Usage

The plugin provides both a command and a default mapping:

- `:gptme` - Prompts for input and runs gptme with context
- `<Leader>g` - Same as :gptme (usually `\g`)

When invoked, it will:
1. Prompt for your input
2. Get context from around your cursor
3. Run gptme with both inputs
4. Show the result in a new buffer

## Configuration

You can configure the following settings in your `.vimrc`:
### Context Lines

Set the number of context lines to include before and after cursor (default: 3):

let g:gptme_context_lines = 5

### Key Mappings

Disable default key mappings:

let g:gptme_no_mappings = 1

If you disable the default mappings, you can set your own:

nnoremap <Leader>G :gptme<CR>

## License

Same as Vim itself - see `:help license`
42 changes: 42 additions & 0 deletions scripts/vim/doc/gptme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
*gptme.txt* gptme integration for Vim

Author: Erik Bjäreholt
License: Same terms as Vim itself (see |license|)

INTRODUCTION *gptme*

This plugin provides integration with gptme (https://gptme.org), allowing you to
interact with gptme directly from Vim.

USAGE *gptme-usage*

The plugin provides both a command and a default mapping:

:Gptme Prompts for input and runs gptme with context
<Leader>g Same as :Gptme

When invoked, it will:
1. Prompt for your input
2. Get context from around your cursor
3. Run gptme with both inputs
4. Show the result in a new buffer

CONFIGURATION *gptme-config*

*g:gptme_context_lines*
Number of context lines to include before and after cursor (default: 3)
>
let g:gptme_context_lines = 5
<

*g:gptme_no_mappings*
Disable default key mappings (default: 0)
>
let g:gptme_no_mappings = 1
<

ABOUT *gptme-about*
gptme.vim plugin version 0.1
Latest version at: https://github.com/ErikBjare/gptme

vim:tw=78:ts=8:ft=help:norl:
71 changes: 71 additions & 0 deletions scripts/vim/plugin/gptme.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
" gptme.vim - gptme integration for Vim
" Maintainer: Erik Bjäreholt
" Version: 0.1

if exists('g:loaded_gptme')
finish
endif
let g:loaded_gptme = 1

" Default settings
if !exists('g:gptme_context_lines')
let g:gptme_context_lines = 3
endif

function! s:gptme() range
" Check if range was given (visual selection)
let l:has_range = a:firstline != a:lastline

if l:has_range
" Get visually selected lines
let l:context = getline(a:firstline, a:lastline)
else
" Get context around cursor
let l:current_line = line('.')
let l:start_line = max([1, l:current_line - g:gptme_context_lines])
let l:end_line = min([line('$'), l:current_line + g:gptme_context_lines])
let l:context = getline(l:start_line, l:end_line)
endif

let l:context_text = join(l:context, "\n")
let l:ft = empty(&filetype) ? '' : &filetype
let l:filename = expand('%:p')

" Now get input from user
let l:input = input('gptme prompt: ')
if empty(l:input)
return
endif

" Build the prompt with proper escaping
let l:prompt = l:input . " at:\n```" . l:ft . "\n" . l:context_text . "\n```"

" Build the command with proper shell escaping
let l:cmd = 'gptme ' . shellescape(l:prompt) . ' ' . shellescape(l:filename)

" Debug: Show command (optional)
echom "Command: " . l:cmd

" Open terminal in a new window
vertical new
file gptme

" Use appropriate terminal function based on Vim/Neovim
if has('nvim')
call termopen(l:cmd)
" Auto-enter insert mode in terminal (Neovim)
startinsert
else
call term_start(l:cmd, {'curwin': 1})
endif
endfunction

" Map it to <Leader>g (usually \g) unless user disabled default mappings
if !exists('g:gptme_no_mappings')
nnoremap <Leader>g :call <SID>gptme()<CR>
" Add visual mode mapping
vnoremap <Leader>g :call <SID>gptme()<CR>
endif

" Command interface (note the capital G)
command! -range Gptme <line1>,<line2>call <SID>gptme()

0 comments on commit a04ee05

Please sign in to comment.