Skip to content

Instantly share code, notes, and snippets.

@traveaston
Forked from vitalybe/tab.bash
Last active October 14, 2019 07:14
Show Gist options
  • Save traveaston/68c1cec3057ba229bfbf2c2e54e32808 to your computer and use it in GitHub Desktop.
Save traveaston/68c1cec3057ba229bfbf2c2e54e32808 to your computer and use it in GitHub Desktop.
Open new iTerm tabs via command line
# tab function has been modified to integrate into basherk / bashrc file.
# dependant function/export taken from basherk https://github.com/traveaston/basherk
# assumes cd lists directory or has some other noisy output which we'd like to avoid here
# ignore commands with leading space
export HISTIGNORE=" *:$HISTIGNORE"
# credit: https://stackoverflow.com/a/17841619
# this solution avoids appending/prepending delimiter to final string
function array_join() {
[[ -z $1 ]] || [[ $1 == "--help" ]] && {
echo "Join array elements with a (multi-character) delimiter"
echo "Usage:"
echo " array_join [--help]"
echo " array_join delimiter \${array[@]}"
return
}
# capture delimiter in variable and remove from arguments array
local delimiter="$1"
shift
# echo first array element to avoid prepending it with delimiter
echo -n "$1"
shift
# prepend each element with delimiter
printf "%s" "${@/#/$delimiter}"
}
[[ $os == "macOS" ]] && {
# credit Justin Hileman - original (http://justinhileman.com)
# credit Vitaly (https://gist.github.com/vitalybe/021d2aecee68178f3c52)
function tab() {
[[ $1 == "--help" ]] && {
echo "Open new iTerm tabs from the command line"
echo "Usage:"
echo " tab Opens the current directory in a new tab"
echo " tab [PATH] Open PATH in a new tab (includes symlinks)"
echo " tab [CMD] Open a new tab and execute CMD (also sets tab title)"
echo " tab [PATH] [CMD] ... You can prob'ly guess"
return
}
local commands=()
local path="$PWD" path_test
local exec_set_title exec_commands user_command
# test if we can cd into $1, and capture as $path if so.
# this way we can handle cases where you're inside a symlinked folder,
# but [[ -d ../foo ]] actaully references the literal parent folder
# more info: https://github.com/traveaston/basherk/commit/3c01046
path_test=$(if command cd "$1" >/dev/null 2>&1; then pwd; fi;)
if [[ -n $path_test ]]; then
path="$path_test"
shift
fi
user_command="$*"
# no need to cd if goal is home directory
[[ $path != "$HOME" ]] && {
commands+=("command cd '$path'")
}
commands+=("clear" "pwd")
[[ -n $user_command ]] && {
exec_set_title="set_title '$user_command'"
commands+=("$user_command")
commands+=("set_title")
}
exec_commands=$(array_join "; " "${commands[@]}")
# osascript 2-space indentation due to deep nesting
osascript &>/dev/null <<EOF
tell application "iTerm"
tell current window
set newTab to (create tab with default profile)
tell newTab
tell current session
write text " $exec_set_title"
write text " $exec_commands"
end tell
end tell
end tell
end tell
EOF
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment