Open iTerm (OS X) to a Directory from R

August 20, 2014

(This article was first published on Data Driven Security, and kindly contributed to R-bloggers)

Sometimes you need to get to a command prompt from R, whether it’s to wrangle git on the command line or do some package work. RStudio provides ways to do this from menus and hotkeys, but on OS X it sticks you in I’m an iTerm user—and, I really prefer being in that app vs the one Apple ships—so, I wrote a tiny function that will drop you into either the current working directory or a path you specify.

iterm <- function(directory=getwd()) {

  system(paste("osascript -e 'activate application "iTerm"' ",
               " -e 'tell application "System Events" to keystroke "t" using command down' ",
               sprintf(" -e 'tell application "iTerm" to tell session -1 of current terminal to write text "cd %s"'", directory)))


It issues a three line AppleScript telling iTerm to

  • activate (or launch)
  • open a new tab
  • cd to the desired location

Nothing earth shattering, but it saves a few mouse clicks and keystrokes and keeps me in the app I want to be in. It should be pretty straightforward to modify this for other operating systems.

To leave a comment for the author, please follow the link and comment on their blog: Data Driven Security. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)