xonecas.io
7415
Va

vim-ai-complete / commit f21322c

feat: simple ai assisted edit plugin for nvim

xonecas2026-05-24 f21322c6a3ea339a0960e7576b5ff7fb41e8a7a8 parent 9acbaf0

3 files changed

README.md+28−0
@@ -0,0 +1,28 @@
1+# vim-pi-complete
2+
3+A simple Neovim plugin to mimic Cursor's selection prompt and Zed's editor assist feature using the pi coding agent's Non-Interactive mode.
4+
5+## Pi as your LLM
6+
7+Currently the plugin expects `pi` to be installed and ready to use.
8+
9+## How to use
10+
11+Select a visual block/line/selection and then type `:Pi <you prompt here>`. The selection will be replaced with the model's output.
12+You can give any kind of prompt, but the agent has no edit tools, so it can only reply with the new text, and it's instructed to do so.
13+There is no visual feedback on submit, but if an error happens you will see it.
14+
15+Just select, prompt and send. Then hope for the best. Each "prompt" is it's own individual session, there is no continuation (you can resume from pi itself).
16+
17+## How to install
18+
19+I know there are many package managers, so please use any that you know, and if I need to make changes to support it, file an issue or PR.
20+
21+Here's how to do it without any package manager:
22+
23+```bash
24+cd ~/.config/nvim/pack/local/start
25+git clone <this repo>
26+```
27+
28+And restart your nvim :D
lua/pi_complete/init.lua+88−0
@@ -0,0 +1,88 @@
1+-- Create a table that will hold everything this module exposes.
2+-- In Lua plugins, returning a table like this lets other files call M.complete().
3+local M = {}
4+
5+-- Build the prompt that will be sent to the `pi` command-line tool.
6+-- We include both editor context and the text the user selected so the tool knows
7+-- exactly what file it is working on and what text it should transform.
8+local function prompt_for_pi(user_prompt, selected_text)
9+ -- table.concat joins each line below with a newline, producing one readable
10+ -- prompt instead of manually adding "\n" to every string.
11+ return table.concat({
12+ -- Current file name, without directories. Example: "init.lua".
13+ 'filename: ' .. vim.fn.expand('%:t'),
14+ -- Full path to the current file, so the tool has precise context.
15+ 'path: ' .. vim.fn.expand('%:p'),
16+ -- The user's instruction, such as "add comments" or "refactor this".
17+ 'prompt: ' .. user_prompt,
18+ -- A label that separates metadata above from the selected text below.
19+ 'selection:',
20+ -- The actual text selected in Visual mode.
21+ selected_text,
22+ -- The "system" prompt
23+ 'Use the information above to generate the exact replacement of the given text above. Return only the exact replacement text. Do not edit the file directly'
24+ }, '\n')
25+end
26+
27+-- Complete or rewrite the current visual selection using the external `pi` tool.
28+-- `user_prompt` is the instruction typed by the user.
29+-- `has_range` tells us whether Vim passed a visual range to this command.
30+function M.complete(user_prompt, has_range)
31+ -- This command only makes sense when text is selected. If there is no range,
32+ -- show an error and stop before changing anything.
33+ if has_range == 0 then
34+ vim.notify('pi-complete: select text visually first', vim.log.levels.ERROR)
35+ return
36+ end
37+
38+ -- We use register "z" as a temporary clipboard for the selected text and for
39+ -- the replacement. Before touching it, save its contents and type so we can
40+ -- restore the user's register exactly as it was.
41+ local old_z = vim.fn.getreg('z')
42+ local old_z_type = vim.fn.getregtype('z')
43+
44+ -- Re-select the previous Visual selection (`gv`), then yank it into register z
45+ -- (`"zy`). `silent normal!` runs the Normal-mode keys without extra messages or
46+ -- user mappings, making the command predictable.
47+ vim.cmd([[silent normal! gv"zy]])
48+
49+ -- Read the selected text and selection type from register z. The type matters:
50+ -- it tells Vim whether the selection was characterwise, linewise, or blockwise.
51+ local selected_text = vim.fn.getreg('z')
52+ local selected_type = vim.fn.getregtype('z')
53+
54+ -- Call the external `pi` program with a prompt built from the user instruction
55+ -- and the selected text from Vim's current working directory. The command's
56+ -- stdout becomes `output`.
57+ local output = vim.fn.system({
58+ 'sh',
59+ '-c',
60+ 'cd "$1" && shift && exec "$@"',
61+ 'sh',
62+ vim.fn.getcwd(),
63+ 'pi',
64+ '-tools',
65+ 'read',
66+ '-p',
67+ prompt_for_pi(user_prompt, selected_text),
68+ })
69+
70+ -- If the shell command failed, restore register z, report the error, and leave
71+ -- the buffer unchanged.
72+ if vim.v.shell_error ~= 0 then
73+ vim.fn.setreg('z', old_z, old_z_type)
74+ vim.notify('pi-complete: pi failed', vim.log.levels.ERROR)
75+ return
76+ end
77+
78+ -- Put the generated output into register z using the original selection type,
79+ -- then re-select the same text and paste over it from register z.
80+ vim.fn.setreg('z', output, selected_type)
81+ vim.cmd([[silent normal! gv"zp]])
82+
83+ -- Finally, restore register z so this plugin does not clobber the user's data.
84+ vim.fn.setreg('z', old_z, old_z_type)
85+end
86+
87+-- Return the module table so Neovim can require() this file and call M.complete().
88+return M
plugin/pi_complete.lua+21−0
@@ -0,0 +1,21 @@
1+-- Guard against loading this plugin more than once in a Neovim session.
2+if vim.g.loaded_pi_complete == 1 then
3+ return
4+end
5+
6+-- Mark the plugin as loaded so re-sourcing the runtime files is a no-op.
7+vim.g.loaded_pi_complete = 1
8+
9+-- Define :Pi as the public entry point for invoking pi_complete.
10+-- Forward any command arguments and range information to the Lua module.
11+vim.api.nvim_create_user_command('Pi', function(opts)
12+ require('pi_complete').complete(opts.args, opts.range)
13+end, {
14+ -- Accept zero or more arguments after :Pi.
15+ nargs = '*',
16+ -- Allow :Pi to be called with a line range.
17+ range = true,
18+})
19+
20+-- Neovim user commands must start with uppercase, so :pi is a tiny alias.
21+vim.cmd([[cabbrev pi Pi]])