Skip to content

Instantly share code, notes, and snippets.

@vitalybe
Forked from bobthecow/tab.bash
Last active May 11, 2022 08:14
Show Gist options
  • Save vitalybe/021d2aecee68178f3c52 to your computer and use it in GitHub Desktop.
Save vitalybe/021d2aecee68178f3c52 to your computer and use it in GitHub Desktop.
Open new Terminal tabs from the command line
#!/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 window
set newTab to (create tab with default profile)
tell newTab
tell current session
write text "cd \"$cdto\"$cmd"
end tell
end tell
end tell
end tell
EOF
}
@charles-dyfis-net
Copy link

printf -v args '%q ' "$@", if you want to generate your arguments in eval-safe form. Otherwise, you're inviting bugs -- local args="$@" flattens the original array into a string, and a string can't store array boundaries in a safe way without escaping.

To provide an example:

$ set -- "first arg" "second arg" "third arg"
$ args_flat="$@"
$ args_arr=( "$@" )
$ declare -p args_flat args_arr
declare -- args_flat="first arg second arg third arg"
declare -a args_arr='([0]="first arg" [1]="second arg" [2]="third arg")'

Now, if your goal is an eval-safe string, that would look like this:

$ printf -v args_str '%q ' "$@"
$ declare -p args_str
declare -- args_str="first\\ arg second\\ arg third\\ arg "

What this did is generate a chunk of script which, if parsed by a shell, will evaluate back to its original inputs -- with first arg, second arg, and third arg each still recognized as separate words. This is thus something you could safely append to cmd.

@flux627
Copy link

flux627 commented Jun 25, 2016

Charles it's unclear to me as to what the correct way to fix this script to avoid vulnerabilities.

@bitsofinfo
Copy link

Is there a way to "name" each tab?

@MrBenJ
Copy link

MrBenJ commented Sep 30, 2016

Hey dude, thank you so much for this. I'm going to be using this a ton. Appreciate the time it took to fork @bobthecow 's work and make it work with iTerm2.

Happy coding!

@michaelblyons
Copy link

@bitsofinfo I don't know where exactly it fits up there, but echo -ne "\033]0;${TAB_NAME}\007" should put you on the right track.

@joncode
Copy link

joncode commented Jun 7, 2019

Is there a way to "name" each tab?

function tabname {
printf "\e]1;$1\a"
}

then alias tn to tabname and its super fast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment