» 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/screen
while already being inscreen
, it doesn't start a newscreen
process -- instead, it performs the requested operation (in this case, launching a process (bash
) insidescreen
) in the samescreen
instance, which is pretty much the same as.. Firefox is doing;D
); note thatscreen
notices that you are already inside ascreen
session because the environment variableSTY
is set and points to the name of thescreen
session you're in bash
can be passed a command to execute (instead of going into an interactive shell) using the-c
switch; in this case, we askbash
to runcd "$PWD" && exec $SHELL --login
and exit- the internal bash variable
PWD
contains the name of the current directory - the environment variable
SHELL
is set bylogin
and 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