Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ the search commands, the sha printed on stdout and the exit codes.

## [Unreleased]

### Added

- A Neovim plugin, in this repository: `:Levi` opens levi in a floating window and shows the
commit you pick in a new tab (in diffview.nvim when it's installed), and `:LeviSend` types
a reference to what you picked into your agent's Neovim terminal, unsent, like the herdr
and tmux plugins. Install `rsdlt/levi` with lazy.nvim or `vim.pack`; Neovim 0.11 or newer.

## [0.20.0] - 2026-10-21

### Added
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,27 @@ In your agent's pane, press `prefix G`. levi pops up over it, in the pane's dire
types the reference into the pane, unsent. See [tmux/README.md](tmux/README.md) for another
key, or for tmux without TPM.

### With Neovim

Run your agent in a Neovim terminal, and the plugin in this repository hands it what you
find. With [lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
{ "rsdlt/levi", keys = { { "<leader>ga", "<cmd>LeviSend<cr>" } } }
```

`:LeviSend` opens levi in a floating window; `Enter` types the reference into the agent's
terminal, unsent. `:Levi` shows the commit you pick instead. See
[docs/neovim.md](docs/neovim.md).

## Joint operations

`C-o` opens the file in your editor at the line you're reading. Quit the editor and levi waits
where you left him. Inside Neovim's terminal, the file opens in a new tab of that Neovim. A
file you deleted since then opens read-only, as it was in that commit.

To run levi in a floating window inside Neovim, see [docs/neovim.md](docs/neovim.md).
To run levi in a floating window inside Neovim, `:Levi` does it: see
[docs/neovim.md](docs/neovim.md).

## Field report

Expand Down
98 changes: 65 additions & 33 deletions docs/neovim.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
# levi in Neovim

This puts levi in a floating window on `<leader>gl` (Neovim 0.11 or newer). Pick a commit with
`Enter` and it opens in a new tab with `git show`. Inside the window, `C-o` opens files in
this same Neovim, in a new tab.
> *You live in that editor. Fine. levi comes to you.*

Add it to your config, in `init.lua` or a file under `plugin/`:
This plugin opens [levi](https://github.com/rsdlt/levi) in a floating window over Neovim, in
the repository of the file you're in. It has two orders:

- **`:Levi`** shows the commit you pick, in a new tab: with
[diffview.nvim](https://github.com/sindrets/diffview.nvim) when it's installed, as
`git show` otherwise.
- **`:LeviSend`** hands what you found to your coding agent. Run the agent in a Neovim
terminal, find the commit, the file and the lines you mean, and press `Enter`. levi types a
reference to them into the agent's input. He doesn't send it. You finish the question.

```
In commit d3a93ab ("Stack the lists above the diff on narrow terminals"), src/ui.rs lines 27-40: why 120?
```

## Enlist

You need levi 0.16 or newer on your `PATH` and Neovim 0.11 or newer.

```sh
brew install rsdlt/tap/levi # or: cargo install levi-git
```

The plugin is in levi's own repository. With [lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
-- levi in a floating window (Neovim 0.11+). Enter on a commit shows it in a new tab;
-- Ctrl-O inside levi opens files in this Neovim.
vim.keymap.set("n", "<leader>gl", function()
local picked = vim.fn.tempname()
local buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
local width, height = math.floor(vim.o.columns * 0.9), math.floor(vim.o.lines * 0.85)
local win = vim.api.nvim_open_win(buf, true, {
relative = "editor", border = "rounded", width = width, height = height,
col = math.floor((vim.o.columns - width) / 2), row = math.floor((vim.o.lines - height) / 2),
})
-- levi draws on stderr, so its stdout, the picked sha, can go to a file.
vim.fn.jobstart("levi > " .. vim.fn.shellescape(picked), {
term = true,
on_exit = function(_, code)
if vim.api.nvim_win_is_valid(win) then vim.api.nvim_win_close(win, true) end
local sha = code == 0 and vim.fn.readfile(picked)[1]
vim.fn.delete(picked)
if not sha then return end
vim.cmd("tabnew | setlocal buftype=nofile bufhidden=wipe filetype=git")
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.fn.systemlist({ "git", "show", sha }))
-- Or, with diffview.nvim: vim.cmd("DiffviewOpen " .. sha .. "^!")
end,
})
vim.cmd.startinsert()
end, { desc = "levi: browse commits" })
{
"rsdlt/levi",
cmd = { "Levi", "LeviSend" },
keys = {
{ "<leader>gl", "<cmd>Levi<cr>", desc = "levi: browse commits" },
{ "<leader>ga", "<cmd>LeviSend<cr>", desc = "levi: hand a commit to the agent" },
},
}
```

levi draws on stderr, so the snippet sends its stdout, the picked sha, to a temporary file and
reads it back when levi exits. With diffview.nvim, swap the last lines of `on_exit` for the
`DiffviewOpen` line in the comment.
With Neovim's built-in `vim.pack` (0.12), add `vim.pack.add({ "https://github.com/rsdlt/levi" })`
and the same keys with `vim.keymap.set`. The plugin binds no keys of its own.

## Orders

`:Levi` and `:LeviSend` take one revision, like levi does: `:Levi main..HEAD` shows only what
your branch adds to main.

In the window, levi works as everywhere else: type to search, `Tab` between panes, `C-o` to
open a file at the line you're reading, in a new tab of this same Neovim. `Esc` closes the
window and does nothing.

`:LeviSend` types into the terminal you're in, or into the one you used last when you're in a
file. It works with any agent that runs in a Neovim terminal, and with plugins that open the
agent in one. When that terminal is on screen, levi puts you in it, ready to type; when it's
hidden, he types anyway and tells you.

From Lua, `require("levi").open(rev)` and `require("levi").send(rev)` do the same, `rev`
optional.

## Fine print

- levi runs in the repository of the file you're in, or in Neovim's working directory when
you're not in a file.
- A reference is never sent, so a wrong terminal costs a `C-u`.
- The lines are the ones in view, or the ones under your drag selection.
- If levi can't start, say outside a repository, the window stays up with his reason. `q`
closes it.
- Under the hood it's `levi --pick-context` and `chansend()`: see
[lua/levi/init.lua](../lua/levi/init.lua) to build your own variations.

The rest of levi's orders, keys and search syntax are in the
[main README](../README.md).
15 changes: 11 additions & 4 deletions docs/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,17 @@ next commit (killing its git), and late messages from old generations are droppe
- `--all` branches.
- Optional `delta` piping / syntax highlighting.
- Sending what's on screen to a coding agent. Done three ways. Pushed: `levi --pick-context`
plus the herdr plugin in `herdr/` and the tmux plugin (`levi.tmux`, the TPM entry point, and
`tmux/`), which type the pick into the agent's input, unsent (herdr and tmux can send text
to a pane, which no agent offers on its own). Pulled: `levi --focus` (see CLI behaviour),
for any agent, in or out of a multiplexer.
plus the herdr plugin in `herdr/`, the tmux plugin (`levi.tmux`, the TPM entry point, and
`tmux/`) and the Neovim plugin (`plugin/levi.lua` and `lua/levi/`, at the root so plugin
managers install the repository as-is), which type the pick into the agent's input, unsent
(herdr, tmux and Neovim's `chansend()` can send text to a terminal, which no agent offers
on its own). Pulled: `levi --focus` (see CLI behaviour), for any agent, in or out of a
multiplexer.
- **Neovim.** `:LeviSend` types into the terminal buffer you're in, else the live one with
the latest `lastused`, picked before levi's own float (a terminal too) opens. `:Levi`
replaces the old paste-in snippet: Enter shows the commit, in diffview.nvim when it's
there (not for a root commit, which has no `sha^!` range). levi's stdout goes to a temp
file through the shell, since a `term` job has one pty for both streams.
- **tmux.** `prefix G` (tmux 3.8 binds `g` to its move table; `@levi-key` changes it) runs
`display-popup -E -d '#{pane_current_path}'`. tmux doesn't expand formats in the popup's
command, only in `-d`, so the script asks `tmux display-message -p '#{pane_id}'`, which
Expand Down
141 changes: 141 additions & 0 deletions lua/levi/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
-- levi in Neovim: levi in a floating window over the editor.
-- :Levi [rev] Enter shows the commit you picked, in a new tab.
-- :LeviSend [rev] Enter types a reference to what's on screen into your
-- agent's terminal, unsent:
-- In commit d3a93ab ("Stack the lists"), src/ui.rs lines 27-40: _
local M = {}

local function notify(msg)
vim.notify("levi: " .. msg, vim.log.levels.ERROR)
end

-- The repository of the file you're in, or the working directory.
local function root()
return vim.bo.buftype == "" and vim.fs.root(0, ".git") or vim.fn.getcwd()
end

-- 90% by 85% of the editor, centered; recomputed when the editor resizes.
local function float_config()
local width, height = math.floor(vim.o.columns * 0.9), math.floor(vim.o.lines * 0.85)
return {
relative = "editor", width = width, height = height,
col = math.floor((vim.o.columns - width) / 2), row = math.floor((vim.o.lines - height) / 2),
}
end

-- Runs levi with args in a float, in dir, and calls on_pick with what Enter
-- printed. Esc quits without a pick; an error stays on screen until q.
local function run(args, dir, on_pick)
if vim.fn.has("nvim-0.11") == 0 then
return notify("needs Neovim 0.11 or newer")
end
if vim.fn.executable("levi") == 0 then
return notify("not found on PATH. Install it: brew install rsdlt/tap/levi")
end
local out = vim.fn.tempname()
local buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
local config = float_config()
-- 'winborder' (0.11) is the user's choice of border, when they made one.
config.border = vim.o.winborder == "" and "rounded" or nil
local win = vim.api.nvim_open_win(buf, true, config)
local resize = vim.api.nvim_create_autocmd("VimResized", {
callback = function()
if vim.api.nvim_win_is_valid(win) then vim.api.nvim_win_set_config(win, float_config()) end
end,
})

-- levi draws on stderr, so its stdout, the pick, can go to a file.
local cmd = { "levi" }
for _, arg in ipairs(args) do table.insert(cmd, vim.fn.shellescape(arg)) end
table.insert(cmd, "> " .. vim.fn.shellescape(out))
vim.fn.jobstart(table.concat(cmd, " "), {
term = true,
cwd = dir,
on_exit = function(_, code)
vim.api.nvim_del_autocmd(resize)
local picked = vim.trim(table.concat(vim.fn.readfile(out), "\n"))
vim.fn.delete(out)
if code ~= 0 and code ~= 130 then
-- levi said why on its screen; leave it up until it's read.
vim.cmd.stopinsert()
vim.keymap.set("n", "q", function() pcall(vim.api.nvim_win_close, win, true) end, { buffer = buf })
return
end
if vim.api.nvim_win_is_valid(win) then vim.api.nvim_win_close(win, true) end
if code == 0 and picked ~= "" then on_pick(picked) end
end,
})
vim.cmd.startinsert()
end

-- The commit in diffview.nvim when it's installed, else `git show` in a new tab.
local function show(sha, dir)
local function git(...)
return vim.fn.systemlist({ "git", "-C", dir, ... })
end
-- diffview takes a range, and a root commit has no parent to start one at.
git("rev-parse", "--verify", "--quiet", sha .. "^")
if vim.fn.exists(":DiffviewOpen") == 2 and vim.v.shell_error == 0 then
return vim.cmd("DiffviewOpen -C" .. vim.fn.fnameescape(dir) .. " " .. sha .. "^!")
end
local lines = git("show", sha)
vim.cmd.tabnew()
local buf = vim.api.nvim_get_current_buf()
vim.bo[buf].buftype, vim.bo[buf].bufhidden = "nofile", "wipe"
pcall(vim.api.nvim_buf_set_name, buf, "levi://" .. sha:sub(1, 7))
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].modifiable, vim.bo[buf].filetype = false, "git"
end

-- A terminal still running its program.
local function live_terminal(buf)
return vim.bo[buf].buftype == "terminal" and vim.fn.jobwait({ vim.bo[buf].channel }, 0)[1] == -1
end

-- The terminal to type into: the one you're in, or the one you used last.
local function agent_terminal()
local current = vim.api.nvim_get_current_buf()
if live_terminal(current) then return current end
local last
for _, info in ipairs(vim.fn.getbufinfo({ bufloaded = 1 })) do
if live_terminal(info.bufnr) and (not last or info.lastused > last.lastused) then last = info end
end
return last and last.bufnr
end

local function revision(rev)
return (rev and rev ~= "") and { rev } or {}
end

--- Opens levi; Enter shows the commit you picked.
---@param rev string? a revision to browse instead of HEAD, like "main..HEAD"
function M.open(rev)
local dir = root()
run(revision(rev), dir, function(sha) show(sha, dir) end)
end

--- Opens levi; Enter types a reference to what's on screen into the terminal
--- you're in, or the one you used last, and doesn't send it.
---@param rev string? a revision to browse instead of HEAD, like "main..HEAD"
function M.send(rev)
-- Before levi opens: its own window is a terminal too.
local target = agent_terminal()
if not target then
return notify("no terminal to type into. Run your agent in :terminal first")
end
local args = revision(rev)
table.insert(args, 1, "--pick-context")
run(args, root(), function(picked)
if not live_terminal(target) then return notify("the agent's terminal has closed") end
vim.fn.chansend(vim.bo[target].channel, "In " .. picked .. ": ")
local win = vim.fn.bufwinid(target)
if win == -1 then
return vim.notify("levi: typed into " .. vim.api.nvim_buf_get_name(target))
end
vim.api.nvim_set_current_win(win)
vim.cmd.startinsert()
end)
end

return M
11 changes: 11 additions & 0 deletions plugin/levi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- levi's Neovim commands. The work is in lua/levi/init.lua, loaded on first use.
if vim.g.loaded_levi then return end
vim.g.loaded_levi = true

vim.api.nvim_create_user_command("Levi", function(opts)
require("levi").open(opts.args)
end, { nargs = "?", desc = "levi: browse commits, Enter shows one" })

vim.api.nvim_create_user_command("LeviSend", function(opts)
require("levi").send(opts.args)
end, { nargs = "?", desc = "levi: browse commits, Enter types a reference into your agent's terminal" })
Loading