How to make GNU Screen start a new window at the CURRENT working directory?
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 dir 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.
The simple solution is to put the following strings in your ~/.screenrc and then us C-x to open new windows:
bind ^x
bind ^x stuff "screen -X chdir \$PWD;screen^M"
http://www.michaelkelleher.info has more tips for intermediate/advanced screen users.
Since that site seems to have gone away, you can find the archive of it here: Michael Kelleher's Personal Website on Archive.org
I didn't find any solution that would work when you already have a process running in a window, so I came up with my own idea. I added following lines to my .bash_profile:
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.