» GNU Screen: open new window with same working directory
I do this many, many times every single day, and I finally found how to implement it: a shell function to open a new window in
screen which is automatically in the same directory as where you call that function.
Here is the bash code to add to your ~/.bashrc:
function dupscreen {
screen bash -c "cd \"$PWD\" && exec $SHELL --login"
}
alias ,d=dupscreen
When I'm in screen and I need another shell in the same directory (e.g. because I'm opening a spec file in vim and need another shell to test the build of the RPM package with osc), I then just type ,d !
Yes, I know, I'm lazy.
For the record, here is the nitty gritty on how this works:- when you run
/usr/bin/screenwhile already being inscreen, it doesn't start a newscreenprocess -- instead, it performs the requested operation (in this case, launching a process (bash) insidescreen) in the samescreeninstance, which is pretty much the same as.. Firefox is doing;D); note thatscreennotices that you are already inside ascreensession because the environment variableSTYis set and points to the name of thescreensession you're in bashcan be passed a command to execute (instead of going into an interactive shell) using the-cswitch; in this case, we askbashto runcd "$PWD" && exec $SHELL --loginand exit- the internal bash variable
PWDcontains the name of the current directory - the environment variable
SHELLis set byloginand contains the login shell that is configured for your user
dupscreen is telling screen to run bash inside itself, and that bash changes into the current directory (i.e. the directory you're in when you call that function) and then executes $SHELL (which is most probably also bash) from there.
The ,d shell alias is for extra lazyness :)






3 Comments:
Hey thanks, this is just what I needed and you posted this only 6 days before I thought to figure it out.
Ah, thanks, been wondering so long how to get screen not to exit after changing to my favorite directories...
Hi, your article inspired me to improve the method. >>Here I described the way to duplicate window's working directory when running any program (not only shell). It is more complicated, but worth the effort.
Post a Comment
<< Home