Skip to content

feat: open $EDITOR at the line under the diff view - #172

Draft
pablospe wants to merge 28 commits into
dlvhdr:mainfrom
pablospe:editor-line
Draft

pablospe wants to merge 28 commits into
dlvhdr:mainfrom
pablospe:editor-line

Conversation

@pablospe

@pablospe pablospe commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #173

Today o opens the file selected in the tree, always at the top. In a long diff you then have to find the line again by hand, and in the directory view, where several files are shown, o opens the directory.

PR_editor_line

With the OSC 1717 records from #168, diffnav knows which file and line each row shows, so o now opens the editor right there:

  • Which row: the selected row in selection mode, otherwise the top visible row. It takes the first record at or below that row from the lines on screen, the same way feat: keep the scroll position when toggling side-by-side or resizing #168 picks the scroll anchor, so it also works while a re-render is pending.
  • Which line: context and added rows open at their line. Deleted rows open where they would be in the new file, and hunk headers open at the hunk start. A side-by-side row that pairs a deleted line with an added one opens the added line on the right. File headers open just the file.
  • Which file: the file comes from the record, so o in the directory view opens the file under the line. Without a record it falls back to the tree's current node, as today. Outside a git repo the path is made absolute, so a file name like +cmd or --opt isn't read as an editor argument.
  • Editor arguments: a new option, ui.editorLineArg, is a template with {line} and {file}, used only when the line is known. The default, +{line} {file}, works for vim, nvim, nano, emacs and others. VS Code users set -g {file}:{line}, and helix/zed/subl users set {file}:{line}. The template is split into words before the placeholders are filled in, so a path with spaces stays one argument. There's no detection by editor name.
  • Like git, $EDITOR now runs through sh -c '$EDITOR "$@"', so it can carry arguments (e.g. code -w) or a quoted path with spaces. Windows has no sh, so there $EDITOR is the editor's path, as before.
  • No line known: with delta older than 0.20, or no line for the row, o runs $EDITOR <file> exactly as before.

Documented in the README: the example config, the options table and the o key.

Stacked PRs

This PR is stacked on #168 (keep the scroll position), and its diff includes #168's commits until that one merges. To see only this PR's changes (the last 12 commits): pablospe/diffnav@osc1717...editor-line

How Did You Test this Change?

  • Unit tests:
    • The row → (file, line) mapping: a preamble row, file header, hunk, side-by-side d+a rows (including a block's later rows, where d keeps the block start), a lone d row, another file, no record, selection mode, and a directory node during a pending re-render.
    • The editor arguments: the default, VS Code style, a path with spaces, and no line. The sh command is run for real with a quoted editor path that has a space, plus -w, and no $EDITOR. On Windows $EDITOR stays one program path. Outside a repo the path is absolute.
  • go test ./..., go vet ./... and golangci-lint run / golangci-lint fmt (v2.12.2) pass.
  • Manually, with EDITOR set to a script that logs its arguments, in a repo whose path has a space, with delta 0.20.1:
    • Pressed o on context, added, deleted and hunk-header rows, in unified and side-by-side, and in selection mode.
    • Tried both the file view and the directory view, where the file came from the line under the view.
    • Tried editorLineArg: "-g {file}:{line}" with EDITOR="... -w".
  • With delta 0.18.2 first in PATH: no line is ever passed, and o opens the tree node as on main.
  • A non-ASCII file name (ä.txt) opens the right file at the right line, with EDITOR="'/path/my ed/ed' -w".

Images/Videos

With selection on (v), using the default ui.editorLineArg and EDITOR=vim: o on an added line in unified opens vim at line 35. After s, o on a context line opens it at line 33. o on the file header opens just the file.

AI disclosure

Claude Code was used to write the code and tests. I reviewed the changes and tested them manually.

Set OSC1717=V1 for delta and, when its output starts with the handshake,
attach each row's line records to the diff line and strip them from the
text. Older delta ignores the variable and renders as before.
Delta writes the handshake in front of its first row, not on a row of its own.
Remember the diff line at the top of the view (or the selected one) and, once the re-rendered diff is shown, scroll back to the row with the same OSC 1717 record. Keeping the row index drifted, since unified and side-by-side lay out a different number of rows. Without records the index is kept as before.
Resizing first let the viewport clamp its top row to the old row count, so a taller pane near the bottom moved up. Also skip the anchor when no content row is visible.
Remember the top row's offset from the first row of its anchor record, so a preamble, delta's decoration rows and wrapped rows stay where they were instead of snapping to the record.
Re-renders show the same patch, so the exact record is always there.
When the new layout wraps the anchor line over fewer rows, the offset spilled onto the next diff line. Rows above the anchor keep their offset.
A scroll or jump made before the re-render arrives is no longer undone by the anchor taken at the s press or resize.
ScrollUp/Down/Top/Bottom dropped the pending anchor even at the edge of the diff, where they don't move. They now share Update's check.
Scrolling right after s or a resize kept the row index on the new layout, so a relative move landed on another line. The viewer now keeps the lines on screen and re-anchors to the line the move lands on; g and G still go to the top and bottom. currentRecord gives the record of the current row from those lines.
Git quotes non-ASCII names with octal escapes and delta copies them into the record, so a.txt with an umlaut came through as \303\244.txt.
EditorTarget maps the selected or top visible row's OSC 1717 record to a file and new-file line, using the same row choice as the scroll anchor.
o takes the file and line from the row's OSC 1717 record and builds the editor arguments from ui.editorLineArg (default "+{line} {file}"). Without a line it runs $EDITOR <file> as before. $EDITOR may now carry its own arguments, like "code -w".
EditorTarget reads the record from the lines on screen (currentRecord) instead of the node's diff, which is empty until the new render arrives.
A quoted editor path with spaces works again, and arguments like "code -w" still do. Windows has no sh, so there $EDITOR is still split on spaces.
A side-by-side row pairs a deleted line with an added one, and every d record in a block keeps the block's start as its new line. o now takes the row's new-file record, so it opens the line shown on the right instead of the block start.
Without a repo root the file name from the diff went to $EDITOR as is, so a name like "+!cmd" ran a vim command and "--version" was read as an option.
Windows has no sh, and splitting $EDITOR on spaces broke paths like C:\Program Files\..., which worked before. Run it as one program, as before.
@github-actions

This comment has been minimized.

@pablospe
pablospe marked this pull request as draft November 26, 2026 12:37
@pablospe

Copy link
Copy Markdown
Contributor Author

Marked as draft because it depends on #168.
To see only this PR's changes (the last 12 commits): pablospe/diffnav@osc1717...editor-line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Open $EDITOR at the line under the cursor

1 participant