设置mac终端默认目录
我只使用终端(mac)进行git,并且只将git用于一个目录。是否可以将默认目录(打开终端时的目录)设置为我使用 git 的目录,如果可以,如何设置?
I only use terminal (mac) for git, and I only use git for one directory. Is it possible to set the default directory (the directory when terminal is opened) to the directory where I use git, and if so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 Mac OS X Lion 10.7 开始,终端支持恢复,默认情况下会自动恢复退出时打开的终端,恢复其工作目录。因此,您可以打开一个新终端并 cd 到您的 git 目录,然后在退出时保持窗口打开。每次重新打开终端时,终端都会在同一目录中。 (默认情况下,这适用于 bash。如果您使用其他 shell,则需要将 /etc/bashrc 中的代码调整为适合您的 shell。我在对 恢复 Zsh-Terminal (OS X Lion)超级用户。)
您还可以安排终端在特定目录中启动 shell。您可以自定义或创建“设置配置文件”以在启动时发出“cd”命令:
启用“运行命令”和“在 shell 内运行”,然后将命令设置为
cd your_git_directory
。当您使用该配置文件打开新终端时,它将转到您的 git 目录。我建议您使用配置文件列表底部的操作(“齿轮”)菜单复制当前的默认配置文件(如果您从未更改过它,则默认为“基本”),然后自定义该配置文件。
最后,要让它在您打开终端时自动打开具有此配置文件的终端,请设置
您的自定义配置文件。 (在 Lion 上,恢复将恢复您退出时打开的窗口,而不是执行启动操作。正如我提到的,您可以在退出时让此终端保持打开状态,当您再次打开终端时它将恢复。或者,您退出时可以按 Option 修饰键;退出菜单项将更改为“退出并放弃 Windows”,并且下次打开终端时它将执行启动操作。)
As of Mac OS X Lion 10.7, Terminal supports Resume and by default will automatically restore terminals you had open when you quit, restoring their working directories. So, you can just open a new terminal and cd to your git directory, then leave the window open when you Quit. Each time you reopen Terminal, the terminal will be there, in the same directory. (This works for bash by default. If you're using some other shell, you'll need to adapt the code in /etc/bashrc to your shell. I've posted code for zsh in my answer to Resume Zsh-Terminal (OS X Lion) on SuperUser.)
You can also arrange for Terminal to start a shell in a particular directory. You can customize or create a "Settings Profile" to issue a "cd" command when it starts:
Enable "Run command" and "Run inside shell", then set the command to
cd your_git_directory
. When you open a new terminal with that profile, it will go to your git directory.I recommend you Duplicate the current default profile (if you've never changed it, the default is "Basic") using the Action ("gear") menu at the bottom of the profiles list, then customize that profile.
Finally, to have it automatically open a terminal with this profile when you open Terminal, set
to your custom profile. (On Lion, Resume will restore windows that were open when you quit, rather than perform the startup action. As I mentioned, you can just leave this terminal open when you Quit and it will be restored when you open Terminal again. Or, you can press the Option modifier key when quitting; the Quit menu item will change to "Quit and Discard Windows" and the next time you open Terminal it will perform the startup action.)
我喜欢“打开新窗口:相同的工作目录”。我为这个问题找到的所有答案(许多SO)都会通过始终转到新的主目录来打破该设置。以下是我在 .profile (或 .bashrc 等)顶部使用的内容。
这将仅在启动时查看您是否位于
HOME
目录中,如果是,则更改为新的START
目录。这样新窗口就不会自动运行这个命令。唯一需要注意的是,如果您在实际的
HOME
目录中并打开一个新窗口,它将带您进入START
。这是预料之中的。I like to have 'New windows open with: Same Working Directory'. All answers I've found for this question (many SO's) will break that setting by always going to the new home directory. Below is what I use at the top of my .profile (or .bashrc, etc).
This will see if you are in your
HOME
directory only on launch, and if so change to your newSTART
directory. That way new windows won't automatically run this command.The only caveat is if you're in your actual
HOME
directory and open a new window, it will take you toSTART
. Which is expected.尝试 echo "cd $directory" >> ~/.bash_profile
Try
echo "cd $directory" >> ~/.bash_profile
使用窗口组。按照您希望的启动方式排列您的 shell 窗口 - cd 到每个窗口中您想要的目录,设置颜色、Shell - 编辑标题等。然后转到“窗口”-“将 Windows 保存为组”。 ……”。为其命名,选中“终端启动时使用窗口组”。下次开始时,此安排将是您的起点。
Use a Window Group. Arrange your shell window(s) as you'd want them to be on startup - cd to the directory you want in each, set colors, Shell--Edit Title, etc. Then go to Window--"Save Windows as Group...". Give it a name, check "Use window group when Terminal starts". Next time you start, this arrangement will be your starting point.
我修改了@Michael Ozeryansky的答案来解决他提到的警告:
唯一的警告是,如果您在实际的主目录中并打开一个新窗口,它将带您开始。这是预期的。
要使第二个终端窗口选项卡从主页或任何其他目录开始:
I modified @Michael Ozeryansky's answer to solve the caveat he mentioned:
The only caveat is if you're in your actual HOME directory and open a new window, it will take you to START. Which is expected.
To make your second terminal window tab starts from the Home or Any other dir:
如果修改 .zshrc 文件,“文件夹中的新建终端”服务快捷方式将无法在 Mac 上使用。我不推荐。
If you modify the .zshrc file, the “New Terminal in Folder” Service shortcut will not work on Mac. I do not recommend.