如何让 GNU Screen 在当前工作目录中启动一个新窗口?

发布于 2024-08-06 20:57:20 字数 100 浏览 1 评论 0 原文

默认情况下,当您在 GNU Screen 中创建新窗口时,它将在调用 Screen 的目录中启动。我想在 GNU Screen 中当前所在窗口的当前工作目录中启动一个新窗口。我该怎么做?

By default, when you create a new window in GNU Screen, it will start in the directory where Screen is invoked. I want to start a new window in GNU Screen at the current working directory of the window I'm currently in. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

兲鉂ぱ嘚淚 2024-08-13 20:57:20

请参阅 GNU Screen chdir 命令。在 Screen 中创建的所有新窗口都使用此目录作为其初始目录。使用它,您可以执行以下操作:

chdir /home/dan/newscreendir
screen

您的新窗口(以及任何将来创建的窗口)将位于设置的目录中。如果它始终是当前工作目录,您可以在 screenrc 中进行一些设置,以通过一个命令执行此操作。

请参阅 GNU Screen 手册页。是相当全面的。

Screen chdir 命令

Screen 无法访问您的 shell 变量也不执行反引号命令。我能一键完成的最接近方法是使用一个小的 Bash 脚本,如下所示:

screen -X setenv currentdir `pwd`
screen -X eval 'chdir $currentdir' screen

或者更紧凑:

screen -X eval "chdir $PWD"

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:

chdir /home/dan/newscreendir
screen

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:

screen -X setenv currentdir `pwd`
screen -X eval 'chdir $currentdir' screen

Or more compactly:

screen -X eval "chdir $PWD"

screen -X sends the command to the currently running Screen session. The first line creates a variable called currentdir. The second line sends the currentdir to the chdir command and then creates a new Screen window.

心碎的声音 2024-08-13 20:57:20

简单的解决方案是将以下字符串放入 ~/.screenrc 文件中,然后使用 Ctrl + X 打开新窗口:

bind ^x

bind ^x stuff "screen -X chdir \$PWD;screen^M"

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:

bind ^x

bind ^x stuff "screen -X chdir \$PWD;screen^M"

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.

冰葑 2024-08-13 20:57:20

当您已经在窗口中运行进程时,我没有找到任何可行的解决方案,所以我想出了自己的想法。我将以下几行添加到我的 .bash_profile 文件:

scr_cd()
{
    cd $1
    screen -X chdir $PWD
}
if [ "$TERM" == 'screen' ]; then
    alias cd=scr_cd
fi

每次更改目录时,屏幕的工作目录都会更新。有人可能不喜欢这种方法,但它很有魅力。

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:

scr_cd()
{
    cd $1
    screen -X chdir $PWD
}
if [ "$TERM" == 'screen' ]; then
    alias cd=scr_cd
fi

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.

嘴硬脾气大 2024-08-13 20:57:20

也许这是 Byobu 特有的,但只需输入 screen 在当前目录中打开一个新窗口。

Perhaps this is specific to Byobu, but simply typing screen opens a new window in the current directory.

诺曦 2024-08-13 20:57:20

要使 Screen 在当前目录中打开一个新选项卡/窗口,您可以将以下代码添加到您的 .screenrc 文件中:

bind c stuff "screen bash^M"

这将导致 Ctrl + a c 命令在当前窗口/选项卡的目录中打开新选项卡/窗口。

注意:您必须确保 Screen 不会启动 登录 shell”,因为这将导致 shell 在登录 shell 的默认目录而不是当前目录中启动。这意味着在您的 .screenrc 文件中,您的 shell 命令不能包含短划线 ('-') 字符。

例如,这是错误的(即,它将启动登录 shell):

shell -$SHELL

但这是正确的(即,它将启动登录 shell):

shell $SHELL

注释 2:不幸的是,此方法的行为与 Screen 中默认的新窗口/选项卡命令并不完全相同。相反,它将命令写入当前窗口并执行它以创建新窗口/选项卡,因此在某些长时间运行的 shell 进程中它将不起作用。换句话说,只有在可以执行普通 shell 命令时才能执行此键盘快捷键。

注3:如果您希望Screen在当前目录中打开新窗口/选项卡打开登录shell,您可以添加以下代码到您的 .screenrc 文件:

bind c stuff "screen bash -l^M"

To make Screen open a new tab/window in the current directory, you can add the following code to your .screenrc file:

bind c stuff "screen bash^M"

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, your shell command cannot include a dash ('-') character.

For example, this is wrong (i.e., it will start a login shell):

shell -$SHELL

But this is right (i.e., it will not start a login shell):

shell $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:

bind c stuff "screen bash -l^M"
笨笨の傻瓜 2024-08-13 20:57:20

您还可以运行:

screen -X eval "chdir $(pwd)"

或者如果您想在设置 chdir 后立即启动一个新窗口,请使用:

screen -X eval "chdir $(pwd)" screen

You could also run:

screen -X eval "chdir $(pwd)"

Or if you want to start a new window as soon as you set chdir, use:

screen -X eval "chdir $(pwd)" screen
三生殊途 2024-08-13 20:57:20

我有一个近乎完美的 Bash 解决方案。 :)

  1. 如果您从不使用password来设置锁屏密码,只需将其添加到文件$HOME/.bash_profile

     导出 PROMPT_COMMAND='screen -p $WINDOW -X chdir "$PWD"'
    
  2. 您需要密码吗?有了这个:

     # 密码“abc”的摘要为 ID1wIq4l2t7s6
     导出 PROMPT_COMMAND='屏幕 -p $WINDOW -X eval "无密码" "chdir \"$PWD\"" "空闲 0 密码 ID1wIq4l2t7s6"'
    

我只希望Screen的开发者尽快添加环境变量PWD

I have a nearly perfect solution for Bash. :)

  1. If you never use password to set a lockscreen password, just add this to file $HOME/.bash_profile:

     export PROMPT_COMMAND='screen -p $WINDOW -X chdir "$PWD"'
    
  2. Do you need a password? With this:

     # The digest of password "abc" is ID1wIq4l2t7s6
     export PROMPT_COMMAND='screen -p $WINDOW -X eval "password none" "chdir \"$PWD\"" "idle 0 password ID1wIq4l2t7s6"'
    

I just hope the developers of Screen add the environment variable PWD as soon as possible.

无名指的心愿 2024-08-13 20:57:20

如果您希望每次都使用相同的命令,请在 .screenrc 文件中添加一行使用 chdir 命令。

如果您在该会话中正在运行 Screen 会话,则可以键入:

screen -X chdir [argument]

如果不带参数,它将是您的主目录,与键入 cd 的结果相同。

如果您有一个脚本(这是一个编程问答网站)或者在 Screen 之外并且 Screen 正在运行,您可以发出:

`which screen` -x -X chdir [argument]

您可能会在 Screen 中运行一些新进程:

`which screen` -x -X screen [command to run in that directory] [arguments for the command]

In your .screenrc file, add a line that uses the chdir command if you want the same one every time.

If you have a running Screen session inside that session, you can type:

screen -X chdir [argument]

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 screen` -x -X chdir [argument]

Which you'll likely follow with running some new process in Screen with:

`which screen` -x -X screen [command to run in that directory] [arguments for the command]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文