Skip to content

diff: add color-words toggle with ctrl-x keybinding#5209

Closed
KJ7LNW wants to merge 1 commit into
jesseduffield:masterfrom
KJ7LNW:color-words-toggle
Closed

diff: add color-words toggle with ctrl-x keybinding#5209
KJ7LNW wants to merge 1 commit into
jesseduffield:masterfrom
KJ7LNW:color-words-toggle

Conversation

@KJ7LNW

@KJ7LNW KJ7LNW commented Jan 15, 2026

Copy link
Copy Markdown

Description

Adds runtime toggle for word-level diffs using git's --color-words flag. Complements existing whitespace toggle by providing finer-grained diff visualization.

Example

The code in this example is out of context, but it shows how word-level diffing can be used to see what actually changed. This is a sample of what it looks like in lazy git when you apply the word level change and enable it:

image

Type of Change

  • Feature (non-breaking change that adds functionality)

Implementation Details

Mirrors whitespace toggle architecture:

  • Config field git.colorWordsInDiffView (default: false)
  • Controller toggle_color_words_action.go with context exclusions for staging/patch-building
  • Keybinding <c-x> registered in global_controller.go
  • Git commands: added --color-words flag conditionally to diff, show, and stash show commands
  • UI: subtitle displays active modes: "(ignoring whitespace) | (color words)"

Both flags can be enabled simultaneously in git commands without conflict.

Testing

  • Tests pass locally (all 72 unit test packages)
  • Build successful

Enable users to toggle word-level diffs (git --color-words) from within
the application without modifying config files. This provides a more
granular view of changes for detailed code review by highlighting
word-level modifications instead of full-line diffs.

- add ColorWordsInDiffView config option to track state across sessions
- add --color-words flag to all diff command builders (commit, diff, stash, working tree)
- add Ctrl+X keybinding to toggle color-words mode
- prevent toggling in staging contexts where word-level diffs are unsupported
- show "(color words)" subtitle indicator when mode is active
- add translations for keybinding, tooltip, subtitle, and error message

Signed-off-by: Eric Wheeler <lazygit@z.ewheeler.org>
@stefanhaller

Copy link
Copy Markdown
Collaborator

@KJ7LNW Thanks, but this has come up before, and I don't want to add this. See #5147 and #4921 for discussion.

@pogoba pogoba mentioned this pull request Jul 9, 2026
7 tasks
@KJ7LNW

KJ7LNW commented Jul 10, 2026

Copy link
Copy Markdown
Author

@KJ7LNW Thanks, but this has come up before, and I don't want to add this. See #5147 and #4921 for discussion.

I'm sorry to hear that. It sure is cleaner to see this instead of the one below it. The first one looks amazing like magic. It shows me exactly which columns were added to the structure, but the second one is rather messy.

image

versus

image

@stefanhaller

Copy link
Copy Markdown
Collaborator

@KJ7LNW It seems that you didn't read the discussion that I linked to. Of course your second screenshot is terrible, and that's not what I'm suggesting as an alternative. My point is that good custom pagers already do a great job at highlighting such differences out of the box; here's your diff rendered by delta, which is my preferred pager:

CleanShot 2026-07-10 at 15 49 23@2x

I admit that the rewrapped comment at the top is not as nice as in your screenshot, but the table is ok. This is the default diff display, no need to switch modes. For tricker cases where delta doesn't manage to highlight the changes well enough I tend to switch to difftastic, which is similar to --color-words.

If you really want to use --color-words, you can do this today; I just added a paragraph to the docs that explains how, see #5795.

Reasons why I don't want to add this as a builtin toggle like this PR does:

  • it doesn't scale. Some users may want --color-words, others prefer --word-diff, and yet others prefer other different options. Supporting them all results in a mess of keybindings.
  • it doesn't work together with pagers. If you use a custom pager (and I think every lazygit user should), then they need a unified diff as input. They won't know what to do with word-diff input.

@KJ7LNW

KJ7LNW commented Jul 10, 2026

Copy link
Copy Markdown
Author

@KJ7LNW It seems that you didn't read the discussion that I linked to. Of course your second screenshot is terrible, and that's not what I'm suggesting as an alternative. My point is that good custom pagers already do a great job at highlighting such differences out of the box; here's your diff rendered by delta, which is my preferred pager:

I did look at the other PR discussion but I misunderstood: I thought somehow you had a custom PAGER= hook related to lazygit that somehow helped you with formatting via the child's stdout, and that my pull request hack somehow would break that.

CleanShot 2026-07-10 at 15 49 23@2x

I admit that the rewrapped comment at the top is not as nice as in your screenshot, but the table is ok. This is the default diff display, no need to switch modes. For tricker cases where delta doesn't manage to highlight the changes well enough I tend to switch to difftastic, which is similar to --color-words.

If you really want to use --color-words, you can do this today; I just added a paragraph to the docs that explains how, see #5795.

Reasons why I don't want to add this as a builtin toggle like this PR does:

* it doesn't scale. Some users may want `--color-words`, others prefer `--word-diff`, and yet others prefer other different options. Supporting them all results in a mess of keybindings.

* it doesn't work together with pagers. If you use a custom pager (and I think every lazygit user should), then they need a unified diff as input. They won't know what to do with word-diff input.

Awesome. I always prefer the clean and architecturally correct implementation. Thank you for pointing that out.

@KJ7LNW KJ7LNW closed this Jul 10, 2026
@KJ7LNW

KJ7LNW commented Jul 10, 2026

Copy link
Copy Markdown
Author

After writing that comment, I realized there might not be an easy way to toggle with a hotkey. Sometimes line diffs and sometimes word diffs are more useful than the other.

I just read your documentation in #5795: Does Lazygit have a mechanism for toggling pagers (ie, diff style) with a hotkey?

edit: It looks like there's a hotkey for cycling pagers, so perhaps never mind.
edit: Delta is working! thanks.

@stefanhaller

Copy link
Copy Markdown
Collaborator

Yes, it's | for cycling, and in the latest version you can use \ to cycle backwards; useful if you have many pagers configured but usually toggle between the first two.

BTW, we recently realized that "pager" is totally the wrong term to use for these, so it's probably going to change to something like "diff renderer" in a future version.

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.

2 participants