Automate iTerm 2 Setup With Alfred Workflow


I rarely restart my computer, mainly because I hate having to set up all my iTerm tabs again. On any given day, it usually looks something like this:

iterm2-typical

I like to use the split screen feature to run tasks in one and GitHub commands in the other. I also like to label each tab for easy reference. Doing this manually is a little more cumbersome than I’d like, so I fixed up an Alfred workflow so now all I have to do is use a quick keyword trigger:

Alfred iTerm2 Workflow


If you want to skip the manual setup, download a copy of the workflow here.

Step 1 | .bash_profile alias

Open you .bash_profile file, typically with open  ~/.bash_profile in your terminal. You may set up aliases differently.

Create an alias, such as alias venture='cd ~/Documents/Projects/shopify-themes/Venture'. Now typing venture in iTerm will quickly navigate you to that folder. This is extra great for long paths or folders with spaces in them.

Run source ~/.bash_profile or open a new terminal window for your alias to become active.

Step 2 | Alfred Workflow

Open Alfred’s preferences window and select the Workflows tab at the top. At the bottom of the list of workflows, click the plus icon to create a new one of your own from a blank workflow. Give it a name, and spice it up with icon if you’d like.

First, click the plus icon in the top right of your workflow area and select Inputs Keyword.

alfred-keyword-trigger

Set the keyword you want — I went with it — and a short description. Make sure you set that an argument is required.

alfred-keyword-details

Now create another piece of the workflow to run some AppleScript with Actions > Run NSAppleScript.

alfred-apple-script

Paste the following into your newly created script.

Update (July 18): As of iTerm2 version 3.0.4, we can use a much simplified script.

on alfred_script(q)

tell application "iTerm2"

  activate current session
  create tab with default profile in current window

  tell current session in current window
    set name to (q as string)
    write text (q as string)

    tell split vertically with default profile
      set name to (q as string)
      write text (q as string)
    end tell
  end tell

end tell

end alfred_script

The following code was for iTerm2 version 2.1.4. It no longer works with version 3+, but will leave it here for reference.

on alfred_script(q)

tell application "iTerm"
  make new terminal

  # navigate to keyword
  tell the current terminal
    activate current session
    launch session "Default Session"
    tell the last session
      write text (q as string)
      set name to (q as string)
    end tell

    tell application "System Events" to tell process "iTerm" to keystroke "d" using command down
    activate current session
    tell the current session
      write text (q as string)
      set name to (q as string)
    end tell
  end tell

end tell

end alfred_script

To break that down a little, we do the following:

  • Start talking to iTerm
  • Make a new terminal window
  • Make a session in the terminal window
  • Tell it to write our keyword in the session. This is what links up your Alfred keyword with the alias you created before
  • Set the name of the tab to the same keyword
  • use System Events to create a split screen window
    • The current version of iTerm2 doesn’t have the ability to do this natively, so you have to use keystrokes (cmd + d)
    • The nightly build has the ability, but I haven’t set that up yet
  • Activate the new window
  • Navigate to your alias and set the name

Lastly, link up the keyword to the script like in the gif below and you’re good to go.

alfred-keyword-script



One comment on “Automate iTerm 2 Setup With Alfred Workflow


  1. RMAU June 12th, 2016
    at 6:57 pm

    reply

    This workflow uses a Python script to extract a list of your iTerm profiles, and a bit of Applescript to tell iTerm to start up the selected one.

Leave a Reply