-->

iTerm2 open file

2019-09-15 07:36发布

问题:

iTerm2 has a nice ⌘-click to open a file in your IDE.

The problem is I'm running a web server via Vagrant, so /home/vagrant isn't really a physical directory that iTerm2 can access.

Is there a way I can either:

A.) Map /home/vagrant/ to /Sites in my .bash_profile

B.) Run it through a Find/Replace script that returns the modified path?

Any suggestions? Thanks

回答1:

There is not way to configure it using .bash_profile... but there is a solution inside your iTerm2's preference.

Preferences -> Profiles -> Semantic History. Change to Run command... and fill with:

sh ~/click-handler.sh \1 /home/vagrant /Sites

create ~/click-handler.sh:

old_filepath=$1
old_basepath=$2
new_basepath=$3
new_filepath="${old_filepath/$old_basepath/$new_basepath}"
open $new_filepath

Let's try!



回答2:

Thanks Udlei. You definitely pointed me in the right direction. The problem with "Run Command" is that iTerm2 (currently 3.0.15) doesn't respond to clicks if it doesn't recognize the directory in the filesystem. I changed it to "Always Run Command" and was able to use your code to come up with this. I really appreciate your help.

#!/bin/bash

# iTerm2 > Profiles > Adv -> Semantic History > Always Run Command:
# sh ~/click-handler.sh \1 /home/vagrant /Sites \5

original_filepath=$1
vagrant_basepath=$2
local_basepath=$3
current_pwd=$4

# remove quotes from var_dump if present
original_filepath_temp="${original_filepath%\"}"
original_filepath="${original_filepath_temp#\"}"

# look for /home/vagrant in the $original_filepath
if [[ $original_filepath =~ ^(\/home\/vagrant) ]];
    then
    new_filepath="${original_filepath/$vagrant_basepath/$local_basepath}"
    # echo $new_filepath > ~/Library/Logs/iterm.log
    /usr/local/bin/subl $new_filepath
# default local handling
    else
    original_filepath="$current_pwd/$original_filepath"
    # echo $original_filepath > ~/Library/Logs/iterm.log
    /usr/local/bin/subl $original_filepath
fi