OSX Terminal – Tab and window shortcuts

In a previous post, I shared a tip on Naming your Terminal tabs in OSX Lion. I’ve been meaning to share some additional shortcuts that I use day-to-day for opening new tabs and renaming them via the command line.

The workflow

We can rename tabs and windows as before:


# Rename tab
tabname "Funky Tab"

# Rename window
winname "Funky Window"

Or, leave out the name to base it the current directory:


# Rename tab to current directory name
tabname

# Rename window to current directory name
winname

…and to save typing, these are aliased as ‘tn’ and ‘tw’, so this will do the same:


# Rename tab to current directory name
tn

# Rename window to current directory name
wn

Now, for opening a new tab we have:


# Open new tab, cd into same directory as current tab and name it
tab

By default this will open a new tab, cd into the same directory as the current tab and name it based on it’s directory. You can optionally include a path:


# Open new tab, cd into specified directory and name it
tab ./path/to/directory

Of course, there is a lazy version of creating tabs too:


# Shortcut for 'tab'
t

The code

To get this functionality, just drop the following into your ~/.profile file:


function tabname {
  # Will use current dir name if called without arg.
  printf "\e]1;${1-$(basename `pwd`)}\a"
}
function winname {
  # Will use current dir name if called without arg.
  printf "\e]2;${1-$(basename `pwd`)}\a"
}
function tab {
  # Will cd into current dir if called without arg.
  osascript -e 'tell application "Terminal"' \
            -e 'tell application "System Events" to keystroke "t" using {command down}' \
            -e "do script \"cd `pwd` && cd ${1-.} && tabname && clear\" in front window" \
            -e 'end tell' > /dev/null
}
alias t='tab'
alias tn='tabname'
alias wn='winname'

See the Gist here.

Happy tabbing!

Published by

Jamie

Hi, I am the Managing Director of SonicIQ Limited in the UK. I have been working in the web development industry since 1999 and have been running SonicIQ since 2001. Currently Ruby On Rails is my preferred development platform. I am experienced in designing with web standards, HTML5, CSS3 and Javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>