Last active
November 10, 2023 08:47
-
Star
(124)
You must be signed in to star a gist -
Fork
(32)
You must be signed in to fork a gist
-
-
Save bobthecow/757788 to your computer and use it in GitHub Desktop.
Open new Terminal tabs from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Open new Terminal tabs from the command line | |
# | |
# Author: Justin Hileman (http://justinhileman.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` | |
# | |
# 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 | |
function tab () { | |
local cmd="" | |
local cdto="$PWD" | |
local args="$@" | |
if [ -d "$1" ]; then | |
cdto=`cd "$1"; pwd` | |
args="${@:2}" | |
fi | |
if [ -n "$args" ]; then | |
cmd="; $args" | |
fi | |
osascript &>/dev/null <<EOF | |
tell application "iTerm" | |
tell current terminal | |
launch session "Default Session" | |
tell the last session | |
write text "cd \"$cdto\"$cmd" | |
end tell | |
end tell | |
end tell | |
EOF | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Open new iTerm and Terminal tabs from the command line | |
# | |
# Author: Justin Hileman (http://justinhileman.com) | |
# | |
# 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 prolly guess | |
function tab -d "Open the current directory in a new tab" | |
set -l cmd "" | |
set -l cdto (pwd) | |
if test (count $argv) -gt 0 | |
pushd . >/dev/null | |
if test -d $argv[1] | |
cd $argv[1] | |
set cdto (pwd) | |
set -e argv[1] | |
end | |
popd >/dev/null | |
end | |
if test (count $argv) -gt 0 | |
set cmd "; $argv" | |
end | |
switch $TERM_PROGRAM | |
case 'iTerm.app' | |
osascript 2>/dev/null -e " | |
tell application \"iTerm\" | |
tell current terminal | |
launch session \"Default Session\" | |
tell the last session | |
write text \"cd \\\"$cdto\\\"$cmd\" | |
end tell | |
end tell | |
end tell | |
" | |
case 'Apple_Terminal' | |
osascript 2>/dev/null -e " | |
tell application \"Terminal\" | |
activate | |
tell application \"System Events\" to keystroke \"t\" using command down | |
repeat while contents of selected tab of window 1 starts with linefeed | |
delay 0.01 | |
end repeat | |
do script \"cd \\\"$cdto\\\"$cmd\" in window 1 | |
end tell | |
" | |
case '*' | |
echo "Unknown terminal: $TERM_PROGRAM" >&2 | |
end | |
end |
ITerm2's Applescript is deprecated. Take a look at the Python API and this example: https://iterm2.com/python-api/tutorial/example.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@newplasticideas there are currently bash and fish implementations. it's possible to port this to zsh as well, but if you're going to do that, i'd suggest starting with https://github.com/oh-my-fish/plugin-tab which has been updated more recently and supports a lot more terminals :)