Vim in a Flash
Supercharging Vim with Gemini Flash Integration
As a Vim enthusiast and AI aficionado, I’ve recently implemented a game-changing feature in my Vim setup: the ability to query Gemini Flash directly from within the editor. This integration allows me to seamlessly enhance my coding and writing workflow by leveraging AI-powered suggestions and improvements. Let me walk you through how I set this up and how it works.
The Setup
The implementation consists of two main components:
- A bash script (
flash.sh
) that handles the API call to Gemini Flash. - A Vim function and keymappings added to the
.vimrc
file.
The Bash Script
The flash.sh
script is responsible for sending requests to the Gemini Flash API. Here’s a breakdown of what it does:
- It defines a function
query_gemini
that takes input text as an argument. - The function constructs a curl command to send a POST request to the Gemini Flash API.
- It processes the API response, extracting the generated text using
jq
. - Finally, it prints the generated text to stdout.
#!/bin/bash
# flash.sh
function query_gemini() {
local input_text="$1"
local api_key="$GEMINI_API_KEY"
local model="gemini-1.5-flash"
local url="https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent"
# Escape special characters in input_text
input_text=$(printf '%s' "$input_text" | jq -sRr @json)
response=$(curl -s -X POST "${url}?key=${api_key}" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [{"text": '"${input_text}"'}]
}]
}')
# Extract the generated text from the JSON response
generated_text=$(echo "$response" | jq -r '.candidates[0].content.parts[0].text' 2>/dev/null)
if [ -z "$generated_text" ]; then
echo "Error: Unable to extract generated text from the API response." >&2
return 1
fi
printf '%s\n' "$generated_text"
}
query_gemini "$1"
The Vim Integration
The Vim integration is achieved through a function called FlashFilter
and two keymappings. Here’s how it works:
- The
FlashFilter
function is called when the user triggers one of the keymappings. - It captures the selected text in visual mode.
- Depending on whether it’s a code query or a regular query, it may prepend a specific prompt to the selected text.
- It then calls the
flash.sh
script with the prepared text. - Finally, it replaces the selected text with the generated output from Gemini Flash.
" add to .vimrc
function! FlashFilter(is_code) range
" Save the current register contents
let save_reg = getreg('"')
let save_regtype = getregtype('"')
" Yank the selected text
normal! gvy
" Get the selected text
let selected_text = getreg('"')
if(a:is_code)
let prefix = "The code snippet below is from a code file being developed in vim and selected by the developer using visual mode, the text in the file will be replaced by your response, augment this code so that it fits in the context of the file. Do not return any markdown or explanation. Do not wrap with backticks ``. :\n"
else
let prefix = ""
endif
" Run the bash script and capture the output
let output = system('~/flash.sh "' . prefix . shellescape(selected_text) . '"')
" Replace the selected text with the output
" execute "normal! gvc" . output . "\<ESC>"
let lines = split(output, "\n")
execute "normal! gvc" . lines[0] . "\<ESC>"
call append(line('.'), lines[1:])
" Restore the original register contents
call setreg('"', save_reg, save_regtype)
endfunction
vnoremap <leader>fc :call FlashFilter(v:true)<CR>
vnoremap <leader>f :call FlashFilter(v:false)<CR>
Using the Shortcut
I’ve set up two keymappings for different use cases:
<leader>f
: For general queries<leader>fc
: For code-specific queries with some prompt engineering
To use these shortcuts:
- Select the text you want to query in visual mode.
- Press
<leader>f
for a general query or<leader>fc
for a code-specific query. - Wait for a moment as Vim sends the request and receives the response.
- The selected text will be replaced with the AI-generated content.
The Magic Behind the Scenes
When you trigger the shortcut, several things happen:
- Vim captures the selected text.
- It calls the
FlashFilter
function, which in turn executes theflash.sh
script. - The script sends a request to the Gemini Flash API with your selected text (and any additional prompt engineering for code queries).
- The API responds with generated text.
- Vim replaces the selected text with the API’s response.
Benefits and Use Cases
This integration opens up a world of possibilities:
- Quick code refactoring suggestions
- On-the-fly text improvements and expansions
- Instant answers to programming questions
- Rapid prototyping of ideas
By bringing the power of Gemini Flash directly into Vim, I’ve significantly streamlined my workflow and boosted my productivity.
Conclusion
Integrating Gemini Flash into Vim has been a game-changer for my development and writing process. It combines the efficiency of Vim with the intelligence of AI, creating a powerful synergy that enhances my work in countless ways. If you’re a Vim user looking to supercharge your workflow, I highly recommend giving this integration a try!