kovidgoyal/kitty · error

Failed to compile word_regex %q: %w

Error message

Failed to compile word_regex %q: %w

What it means

In word-diff mode (Word_diff_mode_words), compute_centers compiles a regex before processing chunks. The pattern comes from word_regexp(), which builds from the configured word_regex; if the user's word_regex in kitty.conf is invalid regexp syntax, compilation fails and the formatted error includes the offending pattern and the underlying regexp error.

Source

Thrown at kittens/diff/patch.go:408

		title = strings.TrimSpace(parts[2])
	}
	left, right, _ := strings.Cut(linespec, " ")
	ls, lc := parse_range(left)
	rs, rc := parse_range(right)
	return &Hunk{
		title: title, left_start: ls - 1, left_count: lc, right_start: rs - 1, right_count: rc,
		largest_line_number: utils.Max(ls-1+lc, rs-1+rc),
	}
}

func (self *Patch) compute_centers(left_lines, right_lines []string) error {
	word_mode := conf != nil && conf.Word_diff_mode == Word_diff_mode_words
	var re *regexp.Regexp
	if word_mode {
		var err error
		re, err = word_regexp()
		if err != nil {
			return fmt.Errorf("Failed to compile word_regex %q: %w", conf.Word_regex, err)
		}
	}

	type pair struct {
		chunk *Chunk
		idx   int
	}
	var pairs []pair
	for _, hunk := range self.all_hunks {
		for _, chunk := range hunk.chunks {
			if !chunk.is_context && chunk.left_count == chunk.right_count {
				for i := 0; i < chunk.left_count; i++ {
					pairs = append(pairs, pair{chunk, i})
				}
			}
		}
	}
	if len(pairs) == 0 {

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Fix or remove the `word_regex` setting in kitty.conf.
  2. Test your pattern with Go semantics: `go run` a one-liner using regexp.Compile, or at minimum avoid (?=, (?!, and \1-style backreferences.
  3. Quote the value correctly in kitty.conf so backslashes aren't mangled.

Example fix

# before (kitty.conf)
word_regex (?<=\s)\w+|\w+(?=\s)

# after
word_regex \w+
Defensive patterns

Strategy: validation

Validate before calling

// Go: validate the configured regex before starting the kitten
if conf.Word_diff_mode == Word_diff_mode_words {
    if _, err := regexp.Compile(conf.Word_regex); err != nil {
        return fmt.Errorf("bad word_regex: %w", err)
    }
}

Type guard

func validWordRegex(s string) bool {
    _, err := regexp.Compile(s)
    return err == nil
}

Try / catch

Parse the error text for 'Failed to compile word_regex'; on match, disable word-diff mode or fall back to the default regex, and prompt the user to fix kitty.conf.

Prevention

When it happens

Trigger: Setting `word_regex` in kitty.conf to an invalid Go regexp (e.g. unbalanced parenthesis, unsupported look-around like (?=...) or backreferences, trailing backslash) and enabling word diff mode; Go's RE2 engine rejects PCRE-isms such as lookahead and \1 backreferences.

Common situations: Copying a word_regex from git/diff tooling that uses PCRE syntax (lookaheads, backrefs) which Go's regexp does not support; stray quoting/escaping from shell or config formatting; regex fragments valid in Perl but invalid in RE2.

Related errors


AI-assisted analysis of kovidgoyal/kitty@6d5d0c4406 (2026-08-27). Data as JSON: /api/errors/177f10681c914aea. Report an issue: GitHub.