iTerm2 click handler

dandavison dandavison Last update: Feb 24, 2022

iterm2-dwim is a click handler for iTerm2. The aim is that youcommand-click on any file path, relative or absolute, and it opens thefile in your editor. If there was a line number, your editor goes tothat line. So, compiler/linter output, tracebacks, git output, etc.

Currently Emacs, PyCharm, VS Code and Sublime are supported. To choose whicheditor to use, see settings.py.

The following path-like patterns are supported. For the ones with linenumbers, the file will be opened at that line.

Installation

  1. Clone this repo and run python setup.py develop.

  2. In settings.py, set the absolute path to the command-line utilitythat opens files in youe text editor / IDE. For PyCharm this iscalled charm, for Sublime this is called subl and for Emacsthis is called emacsclient.

  3. Find the absolute path to the iterm2-dwim executable, by runningthe command which iterm2-dwim. For example, on my system, this is/usr/local/bin/iterm2-dwim.

  4. Open iTerm2 settings, click on "Profiles", select your profile, clickon the "Advanced" tab for that profile, and do two things:

  5. In the "Smart Selection" section, click "Edit", click "+" to add a new rule, and enter thefollowing values in the 3 rule fields:

    • Notes: Compiler/Linter output
    • Regular Expression: (\~?/?([[:letter:][:number:]._-]+/+)+[[:letter:][:number:]._-]+/?)(:.+)
    • Precision: Very High

    Now click "Edit Actions", click "+" to add an action, choose "RunCommand" and enter /absolute/path/to/iterm2-dwim \1 \3 as the"Parameter".

  6. In the "Semantic History" section, choose "Run command" and enter/absolute/path/to/iterm2-dwim \1 \4.

  7. Make sure you didn't literally enter /absolute/path/to/ anywhere!The path should be the path from step (3), given by which iterm2-dwim.

  8. (Optional, but relative paths won't be resolved without it):configure your shell prompt so that the current directory is writtento a file named /tmp/cwd every time the prompt is displayed. Forexample, put this line in your ~/.bashrc:

    export PROMPT_COMMAND='echo $PWD > /tmp/cwd'
  9. ⌘-click on things!

Your iTerm2 settings should look something like this:

Optional configuration

  1. To get error message alerts, run brew install terminal-notifierand check it's working with terminal-notifier -message hello.

For Emacs users:

1. Make sure that you are starting the emacs serverin your emacs config file:

(require 'server)(unless (server-running-p) (server-start))

Debugging

This is under development and you will encounter problems initially.Probably, you'll command click on something and nothing will happen.

You can't use ipdb to debug it: the python process is started byiTerm2 and is not attached to your terminal's standard input/output.Similarly, note that the python process inherits its environment fromthe iTerm2 process and thus does not have access to environmentmodifications made in your shell config file.

It writes a log: run tail -f /tmp/iterm2-dwim.log.

If nothing happens and nothing is written to the log, another trick isjust to run it from the command line and see the traceback:

$ iterm2-dwim /some/file.py 'the text that comes after the file path'

Subscribe to our newsletter