tags:

views:

699

answers:

4

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 to do that? Thanks!

+1  A: 

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

"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.

Dan Midwood
Tried but without success. How could you pass the current working directory to "chdir" command? And how could you bind two commands (chdir and screen) to a single key? Seems to me impossible :|
Rio
forgot to mention: screen seems to ignore $PWD if you do chdir $PWD
Rio
I've edited my answer to show how you can do it with a bash script. The screen eval command enables you to chain multiple commands, it's useful when you want multiple commands assigned to one key binding.
Dan Midwood
Cool! This works just as I want. It's even better because it doesn't interfere with the screen hotkeys. I created a script called "src" to launch a new screen. Thanks very much! :)
Rio
No prob. I've actually been using it myself since I posted it. Since it works for you it'd be great if you could accept my answer.
Dan Midwood
Haha, I tried upvote your answer yesterday and it said I need 15 reputation. Then I tried it again today and just noticed the empty check mark below that... guess StackOverflow guys should make a hint somewhere :| anyway, thanks very much! :)
Rio
A: 

Yeeeah this is what i was looking for THX ;)

now my ~/.screenrc is adding something like this to gent0os default /etc/screenrc:

hardstatus lastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
hardstatus alwayslastline "%{wk}%-w%{Gk}[%n %t]%{wk}%+w%=%{Ck}%D %d/%m/%Y - %c:%s"
chdir $HOME/devel
screen -t hfopi
screen -t kdiff3
chdir /
screen -t eix sudo su
screen -t emerge sudo su
chdir $HOME
screen

which prepares the session right from the start for my typical usage scenario ;-) ...now on to sum wicked bash prompt magic ]:)

eMPee584
+1  A: 

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.

Mike
A: 

If you are on linux, you can use the tip mentioned here. It allows starting new screen window with current directory without using shell.

askonecki