Mac OS X中的terminal窗口

是否可以创build一个快捷方式来打开terminal窗口到特定的目录? 我正在使用gcc来构build小型C程序,但是很烦人,不得不导航到我的源代码所在的目录。 无论如何要configuration一个捷径(或任何东西)terminal打开一个给定的目录中的窗口?

更新 :这个问题仍然困扰着我。 最被投票的答案是我刚刚尝试实施的答案。 但是,我显然无法将$ PROJECTDIRpath放在一起。

让我们假设这些文件位于以下位置:

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

我可以到达那里,当我cd自己的每一件事,但我一直在使用通配符:

 cd doc* cd svn* cd * cd * 

我所尝试的是(例如):

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

(再次,代替$ PROJECTDIR。)当我打开terminal窗口,我有权访问文档,所以我想我会创build新的configuration文件时…

您可以在Terminal.app中使用shellconfiguration文件来执行此操作。

  1. selectterminal菜单,进入Preferenes
  2. 从上面的button中select设置
  3. select一个现有的configuration文件或创build一个新的configuration文件
  4. Shell选项卡中,选中Run Command框并inputcd $ PROJECTDIR ,其中$ PROJECTDIR是项目的path。
  5. 检查运行在壳内

现在从New WindowNew Tab菜单中创build一个窗口或者选项卡,并在你的bash shell中执行cd命令,并将你的目录更改为你想要的。

这很方便,因为您可以为不同的任务创build许多不同的configuration文件。 如果要在启动configuration文件时运行大量命令,请将其放在~/bin/或主目录中的其他位置,然后执行这些命令。 你也可以使用分号; 运行多个命令,如果你已经选中了运行里面的shellcheckbox。

您可以在您的主目录中创build一个符号链接(在OS X中是别名,或者在Windows中是快捷方式),默认情况下,您的terminal打开。

假设您的程序源文件位于/ usr / bin / myapps /中。 而你的主目录是/ users / Frank(你可以通过使用path/〜来引用你的主目录)。

当你在你的主目录(/〜)时,运行命令

 ln -s /usr/bin/myapps project 

这个function是创build一个名为“project”的符号链接到/ usr / bin / myapps位置。 然后,您可以cd项目(CD /〜/项目),并在/ usr / bin / myapps。

这是最简单的修复,因为它是暂时的,可以很容易地删除。 我相信你可以

 rm /~/project 

删除符号链接。

你可以在〜/ .profile中使用别名:

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

然后可以像任何命令一样使用别名。 types:

user@host ~: $ mycode

你在那里!

这样你就没有软链接污染你的$ HOME,你可以随时跳到那里。

Stack Overflow有一个类似的问题。

在Mac OS Finder中打开terminal

我个人不喜欢我在这里看到的基于GUI或AppleScript的解决scheme。 我要做的是在OS X下使用最好的unix命令: open 。 例如

 open /usr/bin/screen 

你可以通过改变/usr/bin/screen来适应你自己的命令。 因此,创build一个名为~/srcTerm.sh的文件,并将这些命令放在里面:

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

然后发出: open -a /Applications/Utilities/Terminal.app ~/srcTerm.sh

我知道这看起来很麻烦,所以现在编辑~/.bash_profle和旁边的任何其他alias命令设置一个新的别名,如:

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

下一次login或源文件时,可以使用命令popupsrc作为快捷方式。

现在你可能会对自己说:“但是dlamblin ,假设我已经打开了一个terminal窗口,正在执行命令行,实际上并不是我要求的,我正在寻找像terminal我可以改变行为。“ 对此,我可能会这样说:“好吧,让所有的GUI鼠标在这里find~/srcTerm.sh文件在你的发现者,'获取信息',并将'打开:'选项是'其他…“,然后”启用所有应用程序“,然后select” Terminal应用程序,不要select”全部更改“,现在您可以双击该文件来popupterminal。一个快速文件夹(又名堆栈),甚至可以将它的名字input到聚光灯下(使用Command-Space随处访问),然后按回车键。

有趣的是,我已经使用了迄今为止的所有3个答案;)

这是另一个选项:使用applescript。 例如,您可以创build在当前finder目录中打开窗口的applescript。

我有一堆这样的applescripts,我把它们与Quicksilver(或pipe家)

默认情况下,我使用一个热键,在当前的finder目录中打开一个terminal,但只有当finder是最前面的应用程序。 否则,我只是得到一个新的窗口。 然后我也有脚本,打开ssh会话到某些服务器,并重新连接到一个屏幕会话。 使用-x选项来屏幕,你可以在同一个服务器terminal上看到多个erminal窗口,很棒:)

编辑:

这是我写的脚本,它打开一个新窗口,如果Finder是当前活动的应用程序,进入当前的Finder目录:

 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 

要使用它,打开脚本编辑器并粘贴它。然后保存在方便的地方(我使用〜/库/脚本),并告诉QuickSilver,pipe家或谷歌快速search栏来运行它。 使用QS和巴特勒,您也可以设置全局快捷键。

希望这可以帮助,

Wout的。

真的不是一个解决scheme,但你知道你可以拖放一个文件夹或文件到terminal,它会插入path的项目

例如input“cd”,然后放下文件夹,然后回车

如果问题只是你必须在terminal启动每个会话: cd /a/very/very/annoyingly/long/path/name ,那么我可以想到两个解决scheme。

  1. 把它放在.profile文件的底部(它在你的用户的主目录中):

    cd / a /非常/非常/烦人/长/path/名称

  2. 编辑您的CDPATH 。 这个方法为你提供了快速的tab完全目录变化,例如我看起来像这样,因为我的大部分shell工作都在两个目录“iliumSvn”或“unix.varia”中。

    导出CDPATH ='.:〜:〜/ iliumSvn /:〜/ unix.varia'

我认为,从长远来看,2是更清洁,更好,但要么工作得很好。 1的问题是,如果你的工作习惯发生了变化,你将需要记住删除该行,否则每当你打开一个shell时,它会让你疯狂。

我这样做的方式是用DTerm http://www.decimus.net/dterm.php这样你可以从你正在工作的编辑器(在我的情况下Textmate)击中一个KeyCombo,你得到一个命令行为这个位置。 你可以比发出一个命令(即使)和多数民众赞成它! 如果你想打开一个真正的terminal,那么你可以按下Command + Return而不是Return,并且你的命令在一个新的terminal窗口中被执行。 看起来比摆弄一堆path更容易