How can I make iTerm2 *cd* into a directory by clicking on *ls* results?

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.


Solution 1:

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

Solution 2:

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.