Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Command-drag to Terminal to change directory UNIX
If you hold down the Command key while dragging an icon to a Terminal window, it will cd to either the directory you dragged (if it's a folder) or the directory containing the item you dragged (if it's a file).

I don't know when this feature appeared; it might not be specific to 10.8, but that's where I discovered it.

[kirkmc adds: This is a really neat feature. Also, if you Command-drag an application or other bundle, Terminal will cd to inside that bundle. This is great if you need to access files within an application bundle, for example. The comments are saying that this is indeed 10.8 only.]
    •    
  • Currently 4.00 / 5
  You rated: 5 / 5 (16 votes cast)
 
[13,811 views]  

Command-drag to Terminal to change directory | 14 comments | Create New Account
Click here to return to the 'Command-drag to Terminal to change directory ' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Command-drag to Terminal to change directory
Authored by: aardvarko on Jul 30, '12 09:19:15AM

Narrowing down the timeline... it doesn't do this in Snow Leopard, as far as I can tell.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: azeotropo on Jul 30, '12 09:59:32AM

Not working on Snow Leopard here as well.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: aaronrp on Jul 30, '12 10:44:13AM
I have done the following in Snow Leopard: In my .tcshrc:

alias cdf 'cd "`finder-window-dir`"'
Where the file "finder-window-dir" contains:

tell application "Finder"
	try
		POSIX path of (target of window 1 as alias)
	on error
		POSIX path of (path to desktop folder as alias)
	end try
end tell
This lets me type "cdf" and it will cd to the folder that is the topmost open window in the Finder. (Then I put "cdf" in the "Run command" setting under "Shell" in Terminal.app, so it opens with that folder whenever I open a new Terminal window)

[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: markuswarren on Jul 30, '12 10:43:20AM

I think the "tag" for this needs to change from "unix" to "10.8", as it's certainly not working for me in either 10.7 or 10.8, but does work in 10.8



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: dzurn on Jul 30, '12 11:23:42AM
I'm using zsh in Snow Leopard, so dragging a folder into the Terminal window will paste the path, and zsh will automatically cd into that directory.

In fact, Terminal has help on this specific topic, even though the default bash won't do this auto-cd.

In Terminal's help menu search, start typing "saving time" and you'll see the Apple documentation on this feature in SL.

---
Madness takes its toll.
Please have exact change.


[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: asher on Jul 30, '12 11:30:46AM

For 10.7, and number of earlier versions, dragging an icon to terminal pasted in the path. So I always typed cd and a space before the drag. Very handy. Command drag had the same behavior in those versions as plain drag. I used bash.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: fferitt25 on Jul 31, '12 11:06:05AM

Agreed !

I am in SNL currently and this result occurs when dragging anything to the Terminal. I also type cd + space before hand and it will change me to the filepath pointed to.

So I suppose I'm not understanding the 10.8 function.

When you all do this is it actually opening the Folder Location on the Desktop as opposed to moving you to the directory inside of Terminal ?

And IF SO - why take the time and effort to open Terminal - drag a folder to the Terminal only to open the Folder ? Why not simply double-click the Folder icon or select it and hit Command+O on the keyboard ?

Seems like many of these types of hints are counter-productive and take extra effort - tossed into that category of "we do it because we can" rather than something actually useful.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: TheGS on Aug 01, '12 04:19:13AM
On 10.8, command-dragging the folder icon into the terminal window do these 3 things beyond simply entering in the path to the folder, in this order...
  1. Cancel what's currently on the command line and re-display the prompt (on the next line, etc.)
  2. Enter 'cd' and a space and then the path to the directory and then a semi-colon.
  3. Finally execute the above 'cd' command for you.

No need for you to manually type the 'cd ' and then drag the icon in and then hit enter if all you want to do is cd to that directory; Terminal.app does all that for you if you hold down the command key.

[ Reply to This | # ]

Command-drag to Terminal to change directory
Authored by: gpitts on Aug 08, '12 02:31:35PM

Dragging an icon to get the path in terminal goes all the way back to NeXTStep/Openstep. The cmd-drag thing is apparently new.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: gabester on Jul 30, '12 11:57:01AM

This doesn't work on my 10.7.4 system. So I'm guessing it's a 10.8 "feature"...
g=



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: Elmore on Jul 30, '12 07:55:59PM

Thanks for the helpful info,
Can something like this be done for Final Cut Pro X?



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: poenn on Jul 31, '12 02:45:30AM

Very cool! Iíve been using drag & drop for a while to easily get a path. Being able to cd into a directory without having to type anything is a nice addition to that!



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: ernst_mulder on Jul 31, '12 05:56:43AM

You can also just drag any folder or bundle right onto the Terminal icon in your Dock to open a new window at that location.



[ Reply to This | # ]
Command-drag to Terminal to change directory
Authored by: ccstone on Aug 27, '12 10:33:49PM

Nice hint. I'll use it now and then.

Option-Drag puts the correctly escaped path of the dragged item(s) into the Terminal.

To get to the open Finder window I normally run the following script via hotkey from FastScripts with the Terminal as the active app. It will open a new tab if the current one is busy.

-ccs

try
  
  tell application "Finder"
    if (count of windows) > 0 then
      set winTarget to target of front window as alias
    else
      set winTarget to path to desktop
    end if
  end tell
  
  set winTarget to quoted form of (POSIX path of winTarget)
  
  tell application "Terminal"
    if (busy of front window) = true then
      do script "cd " & winTarget
    else if (busy of front window) = false then
      tell front window
        do script "cd " & winTarget in selected tab
      end tell
    end if
  end tell
  
on error eMsg number eNum
  tell me to display dialog eMsg & return & return & eNum
end try


[ Reply to This | # ]