How can I make GNU Screen start a new window at the CURRENT working directory?

GnuGnu Screen

Gnu Problem Overview


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?

Gnu Solutions


Solution 1 - Gnu

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][2]

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.

[2]: https://www.gnu.org/software/screen/manual/html_node/Chdir.html "Screen chdir command"

Solution 2 - Gnu

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.

Solution 3 - Gnu

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.

Solution 4 - Gnu

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

Solution 5 - Gnu

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"

Solution 6 - Gnu

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

Solution 7 - Gnu

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]

Solution 8 - Gnu

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRioView Question on Stackoverflow
Solution 1 - GnuDan MidwoodView Answer on Stackoverflow
Solution 2 - GnuMikeView Answer on Stackoverflow
Solution 3 - GnuwronanView Answer on Stackoverflow
Solution 4 - GnunthView Answer on Stackoverflow
Solution 5 - Gnustiemannkj1View Answer on Stackoverflow
Solution 6 - GnuEmanView Answer on Stackoverflow
Solution 7 - GnudlamblinView Answer on Stackoverflow
Solution 8 - GnuV.D.DView Answer on Stackoverflow