Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is it possible to to create a shortcut to open a terminal window to a specific directory? I'm using gcc a lot to build small C programs but it is annoying to have to navigate to the directory where my source code is. Is there anyway to configure an short cut (or anything) to terminal to open the window in a given directory?

Update: This problem is still eluding me. The most voted answer is the one I've just tried to implement. But, I'm apparently having trouble putting together what would be the $PROJECTDIR path.

Lets assume that the files are in something like the following location:

 /Documents/SVN Working Copies/School/Term-1/CSC-373/

I can get there when I cd to every thing by itself but I've been using the wild card:

cd doc*
cd svn*
cd *
cd *

What I tried was (things like):

cd /Documents/SVN Working Copies/School/Term-1/CSC-373/

(again, in place of $PROJECTDIR.) When I open the terminal window, I have access to Documents so I figured I would when creating the new profile...

share|improve this question

9 Answers 9

up vote 4 down vote accepted

You can do this with shell profiles in Terminal.app.

  1. Select the Terminal menu, go to Preferenes.
  2. Select Settings from the buttons above.
  3. Either select an existing profile or create a new one
  4. In the Shell tab, check the Run Command box and enter cd $PROJECTDIR where $PROJECTDIR is the path to your project.
  5. Check Run inside shell

Now from the New Window or New Tab menu when you create a window or tab with the profile you modified, it will execute the cd command inside your bash shell and change your directory to the one you want.

This is handy because you can create many different profiles for different tasks. If you want to run a large number of commands when starting up a profile, put then in file in ~/bin/ or somewhere else in your home directory and execute those instead. You can also use the semicolon ; to run multiple commands if you've checked the Run inside shell checkbox.

share|improve this answer
    
I've just attempted to do this soltuion; this seems like the easiest but it keeps telling me that my directory doesn't exist. I'll post an update above but it won't accept going to nested directories like Documents/SVN Working Copies/... It tells me it doesn't exist but I can go there when I do CD Documents; CD SVN Working Copies; etc. –  Frank V Jul 12 '09 at 14:28
1  
If the path contains spaces, you need to either escape the spaces or quote the entire path. It certainly does work for nested directories, it's just a standard bash command. –  Kamil Kisiel Jul 12 '09 at 15:23

You could create a symbolic link (which is like an alias in OS X, or shortcut in Windows) in your home directory, which is by default where your Terminal opens up.

Let's say your program source files are located in /usr/bin/myapps/. And your home directory is /users/Frank (you can reference your home directory by using the path /~ as well).

When you are in your home directory (/~), run the command

ln -s /usr/bin/myapps project

What this does is create a symbolic link called "project" to the location /usr/bin/myapps. You can then cd to project (cd /~/project) and wind up in /usr/bin/myapps.

This is the easiest fix, as it is temporary and can be removed easily. I believe you can just

rm /~/project

To delete the symlink.

share|improve this answer
    
Could I also eventually delete the symbolic link from Finder or is it hidden from Finder? –  Frank V Jun 23 '09 at 15:12
    
Yes, the symbolic link will appear as a folder or file alias in the Finder. Just drag it to the trash and empty it, and all should be well! –  Nick Jacques Jun 23 '09 at 20:02
    
I tried this... I'm not sure why but this didn't work for me... –  Frank V Jun 28 '09 at 21:50

You can use an alias in your ~/.profile:

alias mycode="cd /your/path/goes/here"

An alias can then be used just like any command. Type:

user@host ~: $ mycode

and you're there!

This way you don't have a softlink polluting your $HOME, and you can jump there anytime you like.

share|improve this answer

There's a similar question on Stack Overflow.

Open terminal here in Mac OS finder

share|improve this answer

Funny, I have used all 3 of the answers so far ;)

Here's another option: use applescript. You can create applescripts that open a window in the current finder directory for example.

I have a bunch of applescripts like that and I tied them to hotkeys with Quicksilver (or butler)

by default, I use a hotkeys that opens a terminal in the current finder directory, but only if finder is the frontmost application. Otherwise I simply get a fresh window. Then I also have scripts that open ssh sessions to certain servers and reconnect to a screen session. With the -x option to screen, you can have multiple erminal windows lookng at the same server terminal, great :)

EDIT:

This is the script I wrote which opens a new window, going into the current Finder directory if Finder is the currently active application:

on run
    -- Figure out if we want to do the cd (doIt)
    -- Figure out what the path is and quote it (myPath)
    try
    	tell application "Finder" to set doIt to frontmost
    	set myPath to finder_path()
    	if myPath is equal to "" then
    		set doIt to false
    	else
    		set myPath to quote_for_bash(myPath)
    	end if
    on error
    	set doIt to false
    end try

    -- Figure out if we need to open a window
    -- If Terminal was not running, one will be opened automatically
    tell application "System Events" to set isRunning to (exists process "Terminal")

    tell application "Terminal"
    	-- Open a new window
    	if isRunning then do script ""
    	activate
    	-- cd to the path
    	if doIt then
    		-- We need to delay, terminal ignores the second do script otherwise
    		delay 0.3
    		do script "cd " & myPath in front window
    	end if
    end tell
end run

on finder_path()
    try
    	tell application "Finder" to set the source_folder to (folder of the front window) as alias
    	set thePath to (POSIX path of the source_folder as string)
    on error -- no open folder windows
    	set thePath to ""
    end try

    return thePath
end finder_path

-- This simply quotes all occurrences of ' and puts the whole thing between 's
on quote_for_bash(theString)
    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "'"
    set the parsedList to every text item of theString
    set AppleScript's text item delimiters to "'\\''"
    set theString to the parsedList as string
    set AppleScript's text item delimiters to oldDelims
    return "'" & theString & "'"
end quote_for_bash

To use it, open Script Editor and paste it in. Then save it somewhere convenient (I use ~/Library/Scripts) and tell QuickSilver, Butler or Google Quick Search Bar to run it. With QS and Butler you can set global shortcut keys as well.

Hope this helps,

Wout.

share|improve this answer
    
How would I implement the AppleScript solution? I know nothing about AppleScript. Technically, I'm a windows software developer but I wanted to venture away from Windows... So, I setup a Linux Unbutu machine and then eventually bought a MacBook because I had some isues with Linux... Anyway, could you provide an example script? –  Frank V Jun 23 '09 at 17:33
    
I edited my answer to include the script, above. Not sure if you got a notification for that so I'm just pointing it out. –  w00t Jun 25 '09 at 11:12

If the problem is simply that you have to start every session in the terminal with this: cd /a/very/very/annoyingly/long/path/name, then I can think of two solutions.

  1. Put this at the bottom of your .profile file (it's in your user's home directory):

    cd /a/very/very/annoyingly/long/path/name

  2. Edit your CDPATH. This method gives you quick tab-completable directory changes Mine looks like this, for example, since most of my shell work is in the two directories 'iliumSvn' or 'unix.varia'.:

    export CDPATH='.:~:~/iliumSvn/:~/unix.varia'

I think that 2 is cleaner and better in the long run, but either should work fine. The issue with 1 is that if your work habits change, you will need to remember to remove that line, or it will drive you crazy every time you open a shell.

share|improve this answer

Not a solution really but did you know you can drag and drop a folder or file in to the terminal and it will insert the path to the item

eg type "cd" and the drop the folder and then hit return

share|improve this answer

The way I do stuff like this is with DTerm http://www.decimus.net/dterm.php this way you can just hit a KeyCombo from the Editor you are working in (Textmate in my case) and you get a Command line for this location. You can than issue a Command (ie make) and thats it! If you want to open a real Terminal there than you can just hit Command+Return instead of Return and your command is executed in this Directory in a new Terminal Window. Seems to me much easier than fiddling with a bunch of Paths etc.

share|improve this answer

Personally I don't like the GUI or AppleScript based solutions I see here. What I'd do is use the best unix command under OS X: open. E.G.

open /usr/bin/screen

You can adapt this by changing /usr/bin/screen to a command of your own making. So make a file called ~/srcTerm.sh and put these commands in it:

#!/bin/sh

cd $HOME #/or/crazy/path/to/src
bash -l #or whatever interactive program you like.

Then issue: open -a /Applications/Utilities/Terminal.app ~/srcTerm.sh

I know that looks cumbersome, so now edit ~/.bash_profle and next to any other alias commands setup a new alias like:

alias popupsrc='open -a /Applications/Utilities/Terminal.app ~/srcTerm.sh'

Next time you login, or source that file you can use the command popupsrc as a shortcut.

Now you might be saying to yourself, "But dlamblin, this assumes I already have a terminal window open and am currently working on the command line. That's really not the case I was asking for. I was looking for something like an icon like Terminal that I can change the behavior of." And to that I might say something like: "Well, get all GUI-mousey here and find the ~/srcTerm.sh file in your finder, 'Get Info' about it, and change the 'Open with:' option to be 'Other...' then 'Enable all applications' and choose the Terminal application. Do not select 'Change All...'. Now you can double click that file to pop up your terminal. You can stick that on your dock, in a quick folder (a.k.a. stack), or even type it's name into spotlight (accessible everywhere with Command-Space) and hit enter."

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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