Keyboard shortcut to open Terminal at the current folder location in Finder
Say I am in a Finder window (eg: ~/Documents/code ) and I want to be able to use a keyboard shortcut to open a terminal window with the prompt
user@computer: ~/Documents/code$
Most of the questions are answered using mouse.
In System Preferences > Keyboard > Shortcuts > Services under Files and Folders there is a New Terminal at Folder shortcut which you can assign a keyboard shortcut to; however, it only appears on the Finder > Services menu, or the right-click context menu, when a Folder is selected. So if you have a Finder window open but no additional folder selected, it is not available. This may be fine for one's particular usage, but if not, then here is an alternative.
For example, say you have a Finder window open to your Downloads folder, which is selected in the Sidebar, and you want to open a Terminal window there. Well the built in service isn't available because an individual Folder isn't selected and what's selected in the Sidebar doesn't count...
One way to open a Terminal window at the location of the current Finder window, using a keyboard shortcut, is to use AppleScript in an Automator Service (Quick Action in macOS Mojave), then assign it a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services > ...
Create an Automator Service adding a Run AppleScript action, then replace the default code with the example AppleScript code shown below.
-
Save the service as e.g. Open Terminal Here with the settings as shown in the image below. Then assign it a keyboard shortcut. It should show under the General section of the aforementioned path.
- Note: After assigning it a keyboard shortcut, you may have to trigger the new service one time with the mouse from the Finder > Services menu for it to work subsequently using the assigned keyboard shortcut.
- Additionally, the assigned keyboard shortcut cannot be one that is already in use by Finder. I wanted to use ^⌘T but it would not work, so I settled for: ^⌘\
- Accessibility privileges for Automator may need to be granted for this service to work properly.
Example AppleScript code:
tell application "Finder"
if exists Finder window 1 then
set currentFolder to target of Finder window 1 as alias
else
return
end if
end tell
tell application "Terminal"
do script "cd " & quoted form of POSIX path of currentFolder & "; clear"
activate
end tell
As an added bonus, you can also use the example AppleScript code saved as an AppleScript application in Script Editor and place it in the Toolbar of Finder, thus allowing one to click the icon to open a Terminal window at the current location when one wants versus using the keyboard short assigned to the Automator service. No third-party app required!
- In Script Editor, save the example AppleScript code as an AppleScript application, as e.g.: Open Terminal Here
- In Finder, give it the same icon as Terminal.
- From Finder, open the Get Info sheet for both the e.g.: Open Terminal Here app and Terminal, by selecting each and pressing: ⌘I
- With both Get Info sheets showing, select the icon in the upper left corner of the one for Terminal and press: ⌘C
- Now select the icon in the upper left corner of the one for e.g.: Open Terminal Here and press: ⌘V
- Close the Get Info sheets.
- Drag an drop, while holding the ⌘ key1, the app bundle for the e.g.: Open Terminal Here app onto the Toolbar in Finder.
1Required in later versions of OS X/macOS.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
There is a 3rd party app named gotoshell available at the Macapp Store for free. This app enables you to put a small icon on your finder window tool bar. When you click the icon, a terminal or iterm2 window opens at the same directory as the finder window. The icon appears on all finder windows. An image of icon is shown below.