Skip to content

Instantly share code, notes, and snippets.

@khaliqgant
Forked from bobthecow/tab.bash
Last active March 16, 2022 08:53
Show Gist options
  • Save khaliqgant/165394415bf508df2fbe to your computer and use it in GitHub Desktop.
Save khaliqgant/165394415bf508df2fbe to your computer and use it in GitHub Desktop.
[Open Terminal Tab From CLI] Open new Terminal tabs from the command line #cli #bash
#!/bin/bash
#
# Open new Terminal tabs from the command line
#
# Original Author: Justin Hileman (http://justinhileman.com)
# Modified By: Khaliq Gant (http://khaliqgant.com)
# Installation:
# Add the following function to your `.bashrc` or `.bash_profile`,
# or save it somewhere (e.g. `~/.tab.bash`) and source it in `.bashrc` or `.zshrc`
#
# Usage:
# tab Opens the current directory in a new tab
# tab [PATH] Open PATH in a new tab
# tab [CMD] Open a new tab and execute CMD
# tab [PATH] [CMD] ... You can prob'ly guess
# Only for teh Mac users
[ `uname -s` != "Darwin" ] && return
# source: https://gist.github.com/bobthecow/757788
# http://stackoverflow.com/questions/1589114/opening-a-new-terminal-tab-in-osxsnow-leopard-with-the-opening-terminal-window
function tab() {
# find which app they're using
app=`echo $TERM_PROGRAM`
cmd=""
cdto="$PWD"
args="$@"
if [ -d "$1" ]; then
cdto=`cd "$1"; pwd`
args="${@:2}"
fi
if [ -n "$args" ]; then
cmd="; $args"
fi
if [[ " ${app} " == *" iTerm.app "* ]]; then
osascript &>/dev/null <<EOF
tell application "$app"
tell current terminal
launch session "Default Session"
tell the last session
write text "cd \"$cdto\"$cmd"
end tell
end tell
end tell
EOF
fi
if [[ " ${app} " == *" Apple_Terminal "* ]]; then
if [ $# -ne 1 ]; then
PATHDIR=`pwd`
else
PATHDIR=$1
fi
/usr/bin/osascript <<EOF
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd $PATHDIR; clear; $cmd;" in (selected tab of win)
end if
end try
end repeat
end tell
EOF
clear
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment