177

One of my primary tools used for programming is my Terminal. It makes my programming process more efficient when I'm able to quickly open a Terminal window.

In Ubuntu, I was using (window+Alt+T) to open Terminal. But now I use a Macbook at my programming job.

Sometimes I use Spotlight to search "Terminal", and press Enter.

I'd like to know if I can assign a keyboard hotkey to do it.

7
  • 44
    I'm voting to re-open this question because it falls under the "tools used primarily for programming". Programmers use the terminal primarily for running programming-related tasks.
    – Greg Kopff
    Commented Oct 12, 2016 at 23:47
  • 1
    @JonKiparsky The question is about having the Terminal open somewhere and opening a new window when another app has the focus. Commented Apr 6, 2017 at 5:09
  • 6
    Just because I think it's ridiculous... I'm reiterating what @GregKopff stated... "unless they directly involve tools used primarily for programming"... Gopal Prasad, the OP, clearly says "One of my primary tools used for programming is my Terminal." Like come on.. a solution to this is highly valuable.
    – mmr118
    Commented Aug 26, 2017 at 22:02
  • 3
    This is a perfectly valid question! There may be many people coming from Linux (using desktops such as Unity, XFCE, Cinnamon, MATE, etc.) looking for functionality like this. The upvotes prove the interest.
    – Socrates
    Commented Jan 11, 2019 at 0:16
  • 1
    @Socrates I agree, but probably AskDifferent is the more ideal location for this question, and SO gets polluted/overused an awful lot. So I have some sympathy with why it was flagged.
    – Benjamin R
    Commented Feb 6, 2019 at 12:34

10 Answers 10

169

I tested the following procedure under macOS Mojave 10.14.6 (18G3020).

Launch Automator. Create a document of type “Quick Action”:

quick action template

(In older versions of macOS, use the “Service” template.)

In the new Automator document, add a “Run AppleScript” action. (You can type “run applescript” into the search field at the top of the action list to find it.) Here's the AppleScript to paste into the action:

on run {input, parameters}
    tell application "Terminal"
        if it is running then
            do script ""
        end if
        activate
    end tell
end run

Set the “Workflow receives” popup to “no input”. It should look like this overall:

workflow with applescript

Save the document with the name “New Terminal”. Then go to the Automator menu (or the app menu in any running application) and open the Services submenu. You should now see the “New Terminal” quick action:

New Terminal service menu item

If you click the “New Terminal” menu item, you'll get a dialog box:

permission dialog

Click OK to allow the action to run. You'll see this dialog once in each application that's frontmost when you use the action. In other words, the first time you use the action while Finder is frontmost, you'll see the dialog. And the first time you use the action while Safari is frontmost, you'll see the dialog. And so on.

After you click OK in the dialog, Terminal should open a new window.

To assign a keyboard shortcut to the quick action, choose the “Services Preferences…” item from the Services menu. (Or launch System Preferences, choose the Keyboard pane, then choose the Shortcuts tab, then choose Services from the left-hand list.) Scroll to the bottom of the right-hand list and find the New Terminal service. Click it and you should see an “Add Shortcut” button:

add shortcut button

Click the button and press your preferred keyboard shortcut. Then, scratch your head, because (when I tried it) the Add Shortcut button reappears. But click the button again and you should see your shortcut:

keyboard shortcut set

Now you should be able to press your keyboard shortcut in most circumstances to get a new terminal window.

17
  • 3
    This works great for me except for when the Terminal app is not yet started. In such a case, then I get two new Terminal windows. I fixed this by changing the AppleScript to the following: on run {input, parameters} tell application "Terminal" if it is running then do script "" end if activate end tell return input end run Commented Jan 9, 2017 at 19:24
  • 4
    @robmayoff I'm having some trouble implementing your script. It works just fine when I click the play button in Automator, however when I click the task under Services, an error pops up saying The action "Run AppleScript" encountered an error. Any ideas? I'm on OSX 10.12.1 if that matters.
    – Axim
    Commented Mar 22, 2017 at 9:58
  • 8
    I was having an issue where it would open a window if Terminal had no windows, but if there was one already open, it would just select that window. All I had to do was remove the word "shell" from the script.
    – LNO
    Commented Jun 7, 2017 at 17:13
  • 3
    I've got windows duplication problem ever with the latest update of this answer. I've fixed it with the following script on run {input, parameters} tell application "Terminal" if not (exists window 1) then reopen activate end tell end run Commented Sep 7, 2017 at 17:05
  • 6
    Running the service directly from Automator always works, but trying to tie it to a keyboard shortcut never works. I even used the super-obscure ctrl-alt-shift-super-T, and still no luck. What's going on here? Commented Jan 21, 2019 at 2:54
24

As programmers we want the quickest, most fool-proof way to get our tools in order so we can start hacking. Here are how I got it to work in macOS 10.13.1 (High Sierra):

  • Option 1: Go to System Preferences | Keyboard | Shortcut | Services. Under Files and Folders section, enable New Terminal at Folder and/or New Terminal Tab at Folder and assign a shortcut key to it. Keyboard shortcut config

  • Option 2: If you want the shortcut key to work anywhere, create a new Service using Automator, then go to the Keyboard Shortcut to assign a shortcut key to it. Known limitation: not work from the desktop

enter image description here

Notes:

  • If the shortcut doesn't work, it might be in conflict with another key binding (and the OS wouldn't warn you), try something else, e.g. if ⇧⌥T doesn't work, try ⇧⌘T.
1
  • 5
    Option 2 worked for me, thanks! Weird limitation of not working from the Desktop. Also I would advise against ⇧⌘T since that is a common shortcut for "reopen last closed tab"
    – Charlie G
    Commented Jan 17, 2018 at 16:08
8

iTerm2 - an alternative to Terminal - has an option to use configurable system-wide hotkey to show/hide (initially set to Alt+Space, disabled by default)

3
  • Speaking of iTerm2. This is what im trying to launch, using automator, but im getting that "script" error when i specify tell application "iTerm2" so how did you open iTerm2?
    – blamb
    Commented Oct 16, 2018 at 19:56
  • 1
    @blamb I usually open iTerm2 via this hotkey (it should be enabled in iTerm2 preferences -> keys -> Hotkey). I've never tried automator.
    – Victor
    Commented Oct 18, 2018 at 11:31
  • For more info on customising this, see this answer: superuser.com/a/1259726
    – Benjamin R
    Commented Feb 6, 2019 at 12:31
3

There is a third party app called hotkey app which can launch apps based on the configured shortcuts.

enter image description here

You can install it and setup required shortcut.

https://codenuts.de/en/posts/hotkey/

1

My favorite way is to install Hammerspoon (via Homebrew) and set the shortcut by pasting this in your config:

--open Terminal
hs.hotkey.bind({"ctrl", "cmd"}, "T", function()
    hs.application.open('Terminal.app')
end)

This brings an existing Terminal to the front or creates a new one if none is currently running.

1

Similarly to other answers, in automator, you can create a Run Shell script action running the command open -a Terminal and then set your desired keybind to the action to your desired keybind in
System Preferences | Keyboard | Shortcut | Services.

By default, this will create a new terminal when you have none opened and focus your existing terminal if you have one opened.
If you set the command to open -a Terminal -n it will create a new terminal every time you use the shortcut but it will create a separate service for each terminal and also a different icon in the Dock.

1
  • 1
    Thank you. Just in case, it should be the 'Quick Action' as you open the Automator. Not the default 'Workflow'. I spent some time trying to figure out why my workflow doesn't appear as a service. Looks like everyone knows this. But I'm not so brilliant, as it turned out. Hope it will help someone too. Commented Jul 2 at 9:08
0

As others mentioned already, use the keyboard shortcut from services. I also had to restart the computer to take into effect.

0

This version of applescriopt will reuse the existing Terminal, while open a new one if not exists.

on run {input, parameters}
    tell application "Terminal"
        if not (exists window 1) then reopen
        activate
    end tell
end run
-12

Try command + t. It works for me.

1
  • This only works when the terminal app is already open or active/focused Commented Feb 12, 2023 at 23:01
-12

mac - "command + t" will open a new terminal window.

1
  • And that is shortcut to open a new window but not a hotkey as asked in the question. Commented Jun 12, 2021 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.