Opening a Terminal at a Specific Folder in macOS

macOS provides several ways to quickly open a Terminal window (or tab) focused on a specific folder, mirroring functionality found in other operating systems. This tutorial covers the built-in features and some popular third-party tools to achieve this.

Using macOS Services (Built-in)

Starting with Mac OS X Lion (10.7), macOS includes a built-in Service that allows you to open a Terminal window at the currently selected folder. This feature might be disabled by default, so you’ll need to enable it first.

  1. Enable the Service:

    • Go to System Preferences > Keyboard > Shortcuts.
    • Select Services in the left sidebar.
    • Find and enable New Terminal at Folder and/or New Terminal Tab at Folder. You can also assign custom keyboard shortcuts for these actions.
  2. Using the Service:

    • In Finder, navigate to the folder you want to open in Terminal.
    • Select the folder (single click to highlight it).
    • You can access the service in a couple of ways:
      • Context Menu: Right-click (or Control-click) on the selected folder. You should see “New Terminal at Folder” or “New Terminal Tab at Folder” in the menu.
      • Services Menu: Go to the Services menu (located in the menu bar, often under the application name). Select the desired option.

    “New Terminal at Folder” will open a new Terminal window focused on that folder. “New Terminal Tab at Folder” will open a new tab within an existing Terminal window (if one is open) or create a new window if no Terminal window is present.

Additional macOS Terminal Features

macOS Terminal offers convenient drag-and-drop functionality:

  • Drag Folder to Terminal Icon: Drag a folder (or path) from Finder onto the Terminal application icon (in the Dock or Applications folder). This will open a new Terminal window at that folder.
  • Drag Folder to Tab Bar: Drag a folder (or path) onto the tab bar of an existing Terminal window. This will open a new tab in that window, automatically changing the directory to the dragged folder.

Using Third-Party Tools

Several third-party tools extend this functionality:

  • cdto: (https://github.com/jbtule/cdto) A small application that integrates with the Finder toolbar, allowing you to quickly open a Terminal window at the selected folder. It supports Terminal, xterm, and iTerm2.
  • DTerm: (http://www.decimus.net/dterm.php) Opens a mini-terminal within Finder (or other applications). It’s highly versatile and compatible with various applications.
  • Open Terminal Here: (http://jo.irisson.free.fr/?p=59) Similar to “Open Command Window Here” in Windows. Offers features like opening a new tab in the frontmost Terminal window when invoked with the Command key.

Automating with Shell Script (Advanced)

For more advanced users, you can create a shell function to change to the top Finder window’s directory directly from the Terminal. Add the following code to your ~/.bash_profile (or equivalent shell configuration file):

function ff { osascript -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "get POSIX path of (target of window ${1-1} as alias)"\
 -e 'else' -e 'get POSIX path of (desktop as alias)'\
 -e 'end if' -e 'end tell'; };

function cdff { cd "`ff $@`"; };

This allows you to type cdff in Terminal to change to the directory of the current Finder window. You can specify a window number as an argument to cdff to switch to a specific Finder window.

Leave a Reply

Your email address will not be published. Required fields are marked *