jesseduffield / jesseduffield/lazygit

Expose line number information to custom command templates

Open
#4,677 2 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
82.4k
Forks
3k
Avg merge
2d 18h
Merged PRs (30d)
19

Description

**Is your feature request related to a problem? Please describe.**

When creating custom commands that need to operate on specific lines in diffs, it would be valuable to have access to the current line number information. While lazygit already tracks line numbers internally (via `pkg/gui/patch_exploring/state.go`), this information is not currently exposed to custom command templates.

This would enable more precise integrations with external tools like linters, analysis tools, or other utilities that benefit from knowing the exact line context.

**Describe the solution you'd like**

I'd like to have access to line number information in custom command templates, such as:

```yaml
customCommands:
- key: 'A'
command: 'my-tool --file={{.SelectedFile.Name}} --line={{.CurrentLineNumber}}'
context: 'files'
description: 'Run tool on current line'
```

Specifically, I'd like template variables like:
- `{{.CurrentLineNumber}}` - The actual line number in the original file
- `{{.SelectedLineInDiff}}` - The position in the diff view (optional)

**Describe alternatives you've considered**

1. **Parsing git diff output externally** - This duplicates lazygit's existing line tracking logic and is less reliable
2. **Using file-level information only** - This works but loses precision for line-specific operations
3. **External tools to analyze cursor position** - More complex to implement and maintain

I noticed that lazygit already has excellent infrastructure for this in the patch exploring system (`CurrentLineNumber()` method), so exposing this to custom commands would leverage existing functionality.

**Additional context**

- Current lazygit version: 0.52.0
- Use case example: I'm building an MCP (Model Context Protocol) bridge that allows AI assistants to interact with lazygit. When implementing this, I realized that having access to line numbers from custom commands would make the integration much more precise and efficient.
- The line tracking functionality already exists in `pkg/gui/patch_exploring/state.go`
- This would enhance lazygit's integration capabilities while maintaining its core simplicity

The line numbers would only be populated when in applicable contexts (diff views, staging) and would be `nil` otherwise, maintaining backwards compatibility.

I can work on a PR if this feature request is accepted.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.