简体   繁体   English

在 MacOS 上重新启动终端而不关闭

[英]Restart terminal without closing on MacOS

How to restart my current MacOS terminal session without closing the window?如何在不关闭 window 的情况下重启我当前的 MacOS 终端 session?

In Linux I use exec bash but it does not work in this environment.在 Linux 我使用exec bash但它在这个环境中不起作用。 I made a few changes to the .bash_profile (prompt, alias etc) I would like to see without closing it and opening again.我对.bash_profile (提示符、别名等)做了一些更改,我希望在不关闭它并再次打开的情况下看到它。

Just type in the command:只需输入命令:

exec bash -l

I guess that should do it.我想应该这样做。

For zsh ,对于zsh

exec zsh -l

This is needed because every shell on macOS by default is a login shell.这是必需的,因为默认情况下macOS的每个 shell 都是登录 shell。

Justing writing exec bash would replace the current shell with a non-login shell which is not the same effect as closing and re-opening the terminal.仅编写exec bash将用非登录 shell 替换当前 shell,这与关闭和重新打开终端的效果不同。

exec would make new bash -l process replace the current shell. exec将使新的bash -l进程替换当前的 shell。 If exec is not used, bash -l would spawn a new shell over the current shell incrementing the $SHLVL .如果不使用execbash -l将在当前 shell 上产生一个新的 shell,增加$SHLVL

For me none of the other solutions work for ZSH.对我来说,其他解决方案都不适用于 ZSH。

Simply source ~/.zshrc did the job actually.只需source ~/.zshrc实际上就完成了这项工作。

Note: running exec zsh -l outputs /Users/my_username/.zprofile:3: command not found: yarn (where my_username is my username).注意:运行exec zsh -l输出/Users/my_username/.zprofile:3: command not found: yarn (其中my_username是我的用户名)。 But running only the command mentioned above does the job.但是只运行上面提到的命令就可以完成这项工作。

If your session is hanging (maybe your SSH connection was interrupted), you won't be able to restart by entering a command.如果你的session挂了(可能是你的SSH连接中断了),你输入命令是无法重启的。

On iTerm, you can navigate to "Session" > "Restart Session" in the menu bar.在 iTerm 上,您可以导航到菜单栏中的“会话”>“重启会话”。

You can also add a key binding for this via "iTerm" > "Preferences" > "Keys" > "Key Bindings" > "+".您还可以通过“iTerm”>“首选项”>“键”>“键绑定”>“+”为此添加键绑定。

  • Keyboard Shortcut: Your choice, I use Cmd-R快捷键:你选,我用Cmd-R
  • Action: "Select Menu Item..." > "Restart Session"操作:“选择菜单项...”>“重新启动会话”

键盘快捷方式示例

The actual answer, assuming you interpret the question as having the same effect at the state of the terminal session as closing and reopening Terminal would, appears to be to run the executable of the used shell to start a new session:假设您将问题解释为在终端会话状态下与关闭和重新打开终端具有相同的效果,实际答案似乎是运行所用 shell 的可执行文件以启动新会话:

https://unix.stackexchange.com/a/217907/137983 https://unix.stackexchange.com/a/217907/137983

zsh

If you're not on Catalina where ZSH is the default shell, it's going to be:如果您不在使用 ZSH 是默认 shell 的 Catalina,它将是:

bash

After this, all state of the previous session (like session environment variables) will be reset.在此之后,上一个会话的所有状态(如会话环境变量)将被重置。 Also ZSH profile should be re-sourced I think.我认为还应该重新获取 ZSH 配置文件。

If you've made any changes to your .bashrc and .bash_profile , then without closing the terminal you can specify alias in your .bashrc and .bash_profile as shown below to restart the terminal:如果您对.bashrc.bash_profile进行了任何更改,那么无需关闭终端,您可以在.bashrc.bash_profile指定alias ,如下所示重新启动终端:

alias rest='exec bash -l;source ~/.bashrc;source ~/.bash_profile'

This command sources the .bashrc and .bash_profile again, in the sense restarts the terminal and creates a new terminal session.此命令再次获取 .bashrc 和 .bash_profile 的来源,从某种意义上说会重新启动终端并创建新的终端会话。 It works for me.这个对我有用。 Give this a try!试试这个!

So, if you wanna restart the terminal, just enter rest (short for restart) in your terminal.因此,如果您想重新启动终端,只需在终端中输入rest (restart 的缩写)即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM