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!

Git add -A and git commit one liner

When working with Git, I often find myself typing:


git add -A && git commit -m "My commit message"

This stages any changes including new files (which "git commit -am" doesn’t), marks any files that have been deleted from the working copy as removed and commits to the repository.

Following a quick Google search, and thanks to this handy git alias article, I created a “ca” (commit all) alias using:


git config --global alias.ca '!git add -A && git commit'

Now committing all changes is as simple as:


git ca -m "My commit message"

This is a good solution, however I prefer adding the following function to my ~/.profile file:


function gca {
  git add -A && git commit -m "$1"
}

Which gets us to:


gca "My commit message"

Quickly pushing your public SSH key to a server

I find myself needing to do this on a regular basis, so here’s a handy snippet for adding your public SSH key to a server’s authorized_keys file, assuming your public key is at “~/.ssh/id_rsa.pub” (the default).


ssh user@host "echo '`cat ~/.ssh/id_rsa.pub`' >> ~/.ssh/authorized_keys"

…or pop this in your ~/.profile file:


function push-key {
  ssh $1 "echo '`cat ~/.ssh/id_rsa.pub`' >> ~/.ssh/authorized_keys"
}

and run the following when you need to push your key to a server:


push-key user@host

Edit: Christopher mentions in the comments that you could use ssh-copy-id which is great on systems that support it, however it’s not available by default in OSX.

Naming your Terminal tabs in OSX Lion

If you find yourself in the Terminal app with a bunch of tabs open, the default name of “bash” isn’t very useful when navigating between them. You can change the tab name via the UI by right clicking the tab, then clicking “Inspect Tab” and changing the window or tab names but this is somewhat long winded.

Below are a couple of bash functions I have in my “.profile” file to make this easier:


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

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

Now you can easily name your tabs or windows with the following:


# Rename tab
tabname "Funky Tab"

# Rename window
winname "Funky Window"

Thanks to Bubu and Chris Page on the SuperUser site for the right codes.

Git – setting up a remote repository and doing an initial push

There is a great deal of documentation and many posts on Git out there, so this is more of a note to self as I keep forgetting the steps needed to set up a remote repository and doing an initial “push”.

So, firstly setup the remote repository:


ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP
exit

On local machine:


cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master

Done!

Team members can now clone and track the remote repository using the following:


git clone git@example.com:my_project.git
cd my_project

Bonus

To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile (I have my current directory displayed in cyan):


function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "