如何让 GNU Screen 在当前工作目录中启动一个新窗口?
默认情况下,当您在 GNU Screen 中创建新窗口时,它将在调用 Screen 的目录中启动。我想在 GNU Screen 中当前所在窗口的当前工作目录中启动一个新窗口。我该怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
默认情况下,当您在 GNU Screen 中创建新窗口时,它将在调用 Screen 的目录中启动。我想在 GNU Screen 中当前所在窗口的当前工作目录中启动一个新窗口。我该怎么做?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
请参阅 GNU Screen
chdir
命令。在 Screen 中创建的所有新窗口都使用此目录作为其初始目录。使用它,您可以执行以下操作:您的新窗口(以及任何将来创建的窗口)将位于设置的目录中。如果它始终是当前工作目录,您可以在
screenrc
中进行一些设置,以通过一个命令执行此操作。请参阅 GNU Screen 手册页。是相当全面的。
Screen chdir 命令
Screen 无法访问您的 shell 变量也不执行反引号命令。我能一键完成的最接近方法是使用一个小的 Bash 脚本,如下所示:
或者更紧凑:
screen -X
将命令发送到当前正在运行的 Screen 会话。第一行创建一个名为currentdir
的变量。第二行将currentdir
发送到chdir
命令,然后创建一个新的 Screen 窗口。See the GNU Screen
chdir
command. All new windows created in Screen use this as their initial directory. Using this, you can do something like:And your new window (along with any future created windows) will be in the set directory. If it's always going to be the current working directory you may be able to set something up in your
screenrc
to do this one in one command.See the GNU Screen man page. It's quite comprehensive.
Screen chdir command
Screen cannot access your shell variable nor execute backticked commands. The closest I can get to doing it in one click is with a small Bash script like this:
Or more compactly:
screen -X
sends the command to the currently running Screen session. The first line creates a variable calledcurrentdir
. The second line sends thecurrentdir
to thechdir
command and then creates a new Screen window.简单的解决方案是将以下字符串放入 ~/.screenrc 文件中,然后使用 Ctrl + X 打开新窗口:
http://www.michaelkelleher.info 为中级/高级屏幕用户提供了更多提示,但由于该网站似乎有消失了,您可以在 Michael Kelleher 在 Archive.org 上的个人网站。
The simple solution is to put the following strings in your ~/.screenrc file and then use Ctrl + X to open new windows:
http://www.michaelkelleher.info had more tips for intermediate/advanced screen users, but since that site seems to have gone away, you can find the archive of it in Michael Kelleher's Personal Website on Archive.org.
当您已经在窗口中运行进程时,我没有找到任何可行的解决方案,所以我想出了自己的想法。我将以下几行添加到我的 .bash_profile 文件:
每次更改目录时,屏幕的工作目录都会更新。有人可能不喜欢这种方法,但它很有魅力。
I didn't find any solution that would work when you already had a process running in a window, so I came up with my own idea. I added following lines to my .bash_profile file:
The screen's working directory is updated every time you change a directory. Someone may not like this approach, but it works like a charm.
也许这是 Byobu 特有的,但只需输入
screen
在当前目录中打开一个新窗口。Perhaps this is specific to Byobu, but simply typing
screen
opens a new window in the current directory.要使 Screen 在当前目录中打开一个新选项卡/窗口,您可以将以下代码添加到您的
.screenrc
文件中:这将导致 Ctrl + a c 命令在当前窗口/选项卡的目录中打开新选项卡/窗口。
注意:您必须确保 Screen 不会启动 登录 shell”,因为这将导致 shell 在登录 shell 的默认目录而不是当前目录中启动。这意味着在您的
.screenrc
文件中,您的shell
命令不能包含短划线 ('-') 字符。例如,这是错误的(即,它将启动登录 shell):
但这是正确的(即,它将不启动登录 shell):
注释 2:不幸的是,此方法的行为与 Screen 中默认的新窗口/选项卡命令并不完全相同。相反,它将命令写入当前窗口并执行它以创建新窗口/选项卡,因此在某些长时间运行的 shell 进程中它将不起作用。换句话说,只有在可以执行普通 shell 命令时才能执行此键盘快捷键。
注3:如果您希望Screen在当前目录中打开新窗口/选项卡并打开登录shell,您可以添加以下代码到您的
.screenrc
文件:To make Screen open a new tab/window in the current directory, you can add the following code to your
.screenrc
file:This will cause the Ctrl + a c command to open new tabs/windows in the directory of the current window/tab.
Note: You must ensure that Screen does not start a login shell by default, because that will cause the shell start in the default directory for a login shell rather than the current directory. This means that in your
.screenrc
file, yourshell
command cannot include a dash ('-') character.For example, this is wrong (i.e., it will start a login shell):
But this is right (i.e., it will not start a login shell):
Note 2: Unfortunately, this method does not behave exactly like the default new window/tab command in Screen. Instead, it writes the command to the current window and executes it to create the new window/tab, so it will not work during some long running shell process. In other words, this keyboard shortcut can only be executed whenever normal shell commands can be executed.
Note 3: If you want Screen to open new windows/tabs in the current directory and open a login shell, you can add the following code to your
.screenrc
file:您还可以运行:
或者如果您想在设置 chdir 后立即启动一个新窗口,请使用:
You could also run:
Or if you want to start a new window as soon as you set chdir, use:
我有一个近乎完美的 Bash 解决方案。 :)
如果您从不使用
password
来设置锁屏密码,只需将其添加到文件$HOME/.bash_profile
:您需要密码吗?有了这个:
我只希望Screen的开发者尽快添加环境变量
PWD
。I have a nearly perfect solution for Bash. :)
If you never use
password
to set a lockscreen password, just add this to file$HOME/.bash_profile
:Do you need a password? With this:
I just hope the developers of Screen add the environment variable
PWD
as soon as possible.如果您希望每次都使用相同的命令,请在
.screenrc
文件中添加一行使用chdir
命令。如果您在该会话中正在运行 Screen 会话,则可以键入:
如果不带参数,它将是您的主目录,与键入 cd 的结果相同。
如果您有一个脚本(这是一个编程问答网站)或者在 Screen 之外并且 Screen 正在运行,您可以发出:
您可能会在 Screen 中运行一些新进程:
In your
.screenrc
file, add a line that uses thechdir
command if you want the same one every time.If you have a running Screen session inside that session, you can type:
Without an argument it will be your home directory, the same result as typing
cd
.If you have a script (this is a programming Q&A site) or are outside Screen and Screen is running, you can issue:
Which you'll likely follow with running some new process in Screen with: