Open a File with Atom from iTerm2 Using Command-Click

One of the lesser known features in iTerm2 is that when you hold the command (⌘) key on your keyboard and click on a filename or a path, it tries to open it.

This can be enabled or disabled in iTerm2 Preferences. iTerm2 Pointer Preferences

This was working fine for me with Sublime editor until I decided to try out Atom. After switching to Atom, my iTerm2 kept opening files in Sublime, so I kind of forgot about this feature. After some annoyance, I decided to resurrect this workflow, because it is especially useful when used in combination with rspec to open failing specs.

The two changes you need to make for this to work are kind of hidden.

1. Atom Shell Commands

First, you need to install Atom Shell Commands by going to the Atom menu → Install Shell Commands. This will install the commands that will allow you to launch Atom from a command line.

2. Configure iTerm2

The ⌘-click behavior is configured per profile.

  1. Open your iTerm2 settings page and click on the Profiles tab.
  2. Select the profile you want to edit and open the Advanced tab.
  3. Select Run command... from the dropdown in the Semantic History.
  4. Enter /usr/local/bin/atom \1:\2 in the text field.

iTerm2 Profiles Preferences

You can customize the behavior by passing different options to this command. For example, if you want to always open your file in a new window, you can pass -n like so: /usr/local/bin/atom -n \1:\2

iTerm2 ⌘-click parameters

  • \1 for filename
  • \2 for line number
  • \3 for text before click
  • \4 for text after click
  • \5 for pwd

Atom CLI parameters

Type atom -h in your terminal to get these options.