Skip to content

Instantly share code, notes, and snippets.

@korya
Last active June 2, 2016 12:55
Show Gist options
  • Save korya/10341712 to your computer and use it in GitHub Desktop.
Save korya/10341712 to your computer and use it in GitHub Desktop.
Shortcut for terminal in MacOS

The approach is based on this answer:

  1. Create a new service in Automator
  2. The service just executes a custom applescript openning a new terminal window
  3. Assign a shortcut to the service

This solution is great, since it does not require to install a third party application for such trivial task. The only problem with it is the applescript provided in the answer uses activate, which steals a focus from current window, which in its turn can lead to switching to a different virtual desktop.

I've just switched to MacOS from Ubuntu, and I'm a big fan of tiled window managers (thanks to WMII). So I use virtual desktops very heavily, and the activate problem is very critical for me. Surprisingly, I've come up with a really simple workaround:

on run {input, parameters}
	tell application "Terminal"
		do script ""
		activate window 1
	end tell
end run

Pros:

  • a new terminal window is created
  • the newly created window is focused

Cons:

  • on a first time (the terminal application is not yet running) 2 windows are created

This is not a critical issue for me, since ussually I've a couple of different shells running around.

Update: I've used a similar approach for iTerm2, the applescript has changed to:

on run {input, parameters}
	tell application "iTerm"
		set myterm to (make new terminal)
		tell myterm
			set number of columns to 105
			launch session "Default"
		end tell
	end tell
end run

Update 2016/06/01: I've just upgraded to the iTerm2 v3.0.0 and had to modify the script to:

on run {input, parameters}
	
	tell application "iTerm"
		set myterm to (create window with profile "Default")
		tell myterm
			tell current session
				set columns to 105
			end tell
		end tell
	end tell
	
end run

PS The only critical unsolved problem for now is how to bind this service to 'Cmd + Enter'. I've got used to this shortcut from the times of WMII, press 'Alt + Enter' and a new terminal window appears. Currently, I've bound the service to 'Cmd + ', which is close enough, but still not the same.

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