Would it be possible to make some sort of remap where command-clicking or highlighting some text in the terminal triggers iTerm2 to attempt to cd <text>? Would make life somewhat easier when navigating around complex directory trees with long names.

I found these instructions for making command-click trigger an action on the clicked filename or word. But after trying the Run command... option with cd \1 (for "cd to this clicked item"), that doesn't seem to work.

The answer below evidently does not work on remote logins; if anyone has a solution that does so, I will accept your answer.

  • cd <kbd>Tab</kbd> will list the directories in the current directory - note this is the shell not iTem2/Terminal that does this – Mark Aug 13 '17 at 14:11
  • Oh I know; but Tab completion often would be slower than being able to click ls results, especially if you have many directories with very similar names (which might require you to hit Tab multiple times before a single directory is isolated, or just type out the full name). – Luke Davis Aug 13 '17 at 20:38
  • (I do modelling for my work, and often have lots of nested directory trees with long and very similar names, that are a pain in the butt to cd into even with tab completion.) – Luke Davis Sep 14 '17 at 4:05
  • You can make Path Finder (and I think Finder) open a terminal in the directory selected – Mark Sep 14 '17 at 10:08

I've found that you need to go Preferences -> Profiles -> Advanced -> Semantic History and use Run coprocess... and echo cd \1 to get the desired result.

Don't ask me why this works or how I found this solution.

enter image description here

  • Damn... doesn't work on remote logins, I guess the coprocess is local then. – Sida Zhou Jan 2 '18 at 2:25
  • Ah, that's unfortunate. Maybe this isn't possible then. – Luke Davis Jan 2 '18 at 6:24
  • I came up with an approach (see answer below) that uses a Paste from Selection keyboard shortcut. Perhaps this is the best we can do. – Luke Davis Jan 2 '18 at 19:02

Here's another approach that should work in remote sessions. Just create the keyboard remap as shown below (go to iTerm2...Preferences...Keys and click on the "Plus" sign, then select "Paste from Selection..." from the drop-down menu for "Action"). Take note of the Replace...With fields.

This way, we can double click on an item to highlight it and press the magic keyboard shortcut. We've now cd'd into a directory without having to type out its name.

It would be possible to do this with a Pointer action instead of a keyboard shortcut, but unfortunately, the Pointer section does not have the same detailed options for "Paste from Selection". Perhaps this will change in a future release.

  • I would prefer this solution, since it works on remote sessions. Who's the wizard now? :D – Sida Zhou Jan 4 '18 at 3:33
  • Thanks :) but it is a bit hacky. Hope they expand the Cmd+Click options in a future version. – Luke Davis Jan 4 '18 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .