Configure iTerm2's Smart Selection for Diff Paths

When working with diffs in the terminal, for example:

--- a/src/pyrefdev/mapping/pylabel.py
+++ b/src/pyrefdev/mapping/pylabel.py

I often need to copy the path of the file. But double-clicking the path selects a/src/pyrefdev/mapping/pylabel.py by default.

Luckily, you can configure iTerm2's Smart Selection with this regex to only select the path part:

(?<=[-+]{3} [ab]/)\S+

Here is a step-by-step guide:

  • Go to Settings > Profiles > Advanced > Smart Selection > Edit.
  • Click the + button.
  • Set Regular expression to (?<=[-+]{3} [ab]/)\S+.
  • Set the precision to Very High so it wins over the built-in path rules.

Now a double-click selects just src/pyrefdev/mapping/pylabel.py.