16

When using Spaces on Mac OS X trying to open a new terminal instance results in focus being pulled to whatever space has the most recently accessed terminal window. With Finder I can click on the Desktop and then use -N to create a new Finder window. What I am looking for is a method for performing the same function for Terminal windows.

How can I create a new Terminal instance in the current space, regardless of whether or not there is another Terminal instance in one or more other spaces.

Thanks.

1
  • i like the question, but think it should be on super user.
    – hvgotcodes
    Oct 15, 2010 at 0:50

5 Answers 5

5

You can create a new service using automator (from the applications folder). At the top of the automator window, enter: Service receives 'no input' in 'any application'. Then, in the workflow, add 'Run applescript'. Make the applescript contain the following:

on run {input, parameters}

    tell application "Terminal"
        do script ""
        activate
    end tell

    return input
end run

(You can add any command you like into the do script line between the " " (e.g. something useful to run when terminal first opens like uptime if you like, or just leave it blank).

Save the service as 'Open Terminal' or whatever name you like. It should now appear in your services menu.

Then if you want to add a keyboard shortcut, you just need to go to System Preferences -> Keyboard -> Keyboard Shortcuts tab, select services in the left hand menu, and find your new service (its probably near the bottom). Just click in the space to the right of your service name, and assign it whatever keyboard shortcut you like, and hey presto! it should all work

4
  • 1
    It should be noted that this does actually provide the requested functionality. As a minor edit, you need the do shell script " " line for it to open a new window in the current space, but there is no need for the shell script to be a blank space. If it's a blank space, it causes the Terminal to run the blank space as a command, leading to execution delays and an extraneous line in the new Terminal window. Just make it a completely empty string: do shell script "" and then it opens a fresh Terminal window but doesn't issue any commands whatsoever in it.
    – Dakota
    Aug 7, 2013 at 3:05
  • Yep, you're right. I've updated the answer to remove the space from between the quotes. However, in order to work it needs to be do script "", and not do shell script "", as @dakota suggested
    – tmdavison
    Nov 28, 2013 at 10:35
  • Can this be generalized to other applications than the Terminal? Say, a hotkey to open Safari or FireFox in a way that ignores the "When switching to an application, switch to…" configuration-option? Jan 24 at 17:31
  • @ELLIOTTCABLE You can find some other applications here: superuser.com/questions/550312/…
    – tmdavison
    Jan 25 at 10:51
3

In the application assignments in System Preferences -> Spaces, either set Terminal to "Every Space" or delete the entry. Then it should be no problem to open a new Terminal window wherever you want, e.g. by clicking on the dock context menu "New Window" (works even if you have the "When switching to an app, switch to space with open windows from that app" option enabled).

3
  • The first sentence is irrelevant, as far as I can tell. Setting it to "Every Space" still changes the Space when opening a new window via something like AppleScript or a keybinding, and the dock context menu never changes the space regardless of this setting. The one interesting thing in this answer is the "When switching to an app, switch to space with open windows from that app" option. Disabling it "fixes" the keybinding/AppleScript case, but unfortunately "breaks" command-tab. If only OS-X had a proper "cycle windows" key binding that worked across all windows in all apps on all spaces. Apr 29, 2011 at 6:34
  • @LaurenceGonsalves, you can cycle through all the Terminal windows across all Spaces with Command-Left/Right Arrow. Does that help?
    – Chris Page
    Sep 22, 2011 at 4:40
  • @Chris That's an interesting set of shortcuts, thanks, but they don't solve the problem I'm referring to. If the "When switching to an app..." setting is disabled and you're on a space with no Terminal windows and you command-tab to "Terminal" it doesn't switch to a space that has a Terminal window, and command-backtick won't do anything. Amazingly, command-left/right-arrow doesn't do anything in this case either (at least on Snow Leopard). Those keys will cycle between windows across spaces if a window in the current application has the focus, but if no window has the focus they do nothing. Sep 25, 2011 at 2:18
3
tell application "Terminal"
    do script ""
    activate
end tell

To make this work under Lion, type the above into a new AppleScript window and save it. Invoke the script through a command launcher like QuickSilver or assign it to a keyboard shortcut. This should work, even if you have set the "When switching to an application, switch to a space with open windows for the application" setting enabled in Mission Control. The trick is to do the activate command last, rather than at the beginning.

2
  • Alternatively, create a Service using Automator and actuvate it using a keyboard shortcut set in System Preferences: apple.stackexchange.com/a/13655/36722
    – Dakota
    Aug 7, 2013 at 3:10
  • This works! I use it in BetterTouchTools to create a trigger to launch a new Terminal session. I'm using "Run Apple Script (async in background)" and it works like charm. Feb 16, 2021 at 15:20
0

If you use the dock menu to open a new window it opens in the current space.

3
  • Is there a way to do this from the keyboard? Apr 28, 2011 at 22:03
  • Control-F3 is "Move focus to the Dock". You can use the arrow keys to navigate through Terminal's Dock icon menu. You can also type to select menu items by name, e.g., type "N" or "New W". See System Preferences > Keyboard > Keyboard Shortcuts > Keyboard & Text Input to see all the keyboard shortcuts for selecting and navigating through UI.
    – Chris Page
    Sep 25, 2011 at 6:42
  • It's possible to do this. You need to create a special Service using Automator, which you can then bind to a keyboard shortcut from System Preferences: apple.stackexchange.com/a/13655/36722
    – Dakota
    Aug 7, 2013 at 3:10
0

You can disable OSX switching spaces automatically when you switch applications using the following commands:

defaults write com.apple.dock workspaces-auto-swoosh -bool NO
killall Dock

Works on 10.6.8

1
  • There's a GUI setting for this too, it's in "Mission Control".
    – SilverWolf
    Jan 19, 2018 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .