Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gnu-screen

gnu

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?

like image 527
Rio Avatar asked Oct 04 '09 21:10

Rio


People also ask

How do I add a new window to my screen?

To create a new screen window, just press “Ctrl-A” and “c“.

What is the command used for simple use of GNU screen?

Log files of current screen sessions can be started with the Ctrl+a H command, which will make a file called screenlog. X where X is the number of your screen session. A screenshot of what is currently in your screen window can be invoked with Ctrl+a h, creating a file called hardcopy.

How do I open an existing screen session?

The process for doing this is surprising simple and involves only a handful of commands. To start a screen session, you simply type screen within your ssh session. You then start your long-running process, type Ctrl+A Ctrl+D to detach from the session and screen -r to reattach when the time is right.


2 Answers

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

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.

like image 136
Dan Midwood Avatar answered Sep 22 '22 04:09

Dan Midwood


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.

like image 32
Mike Avatar answered Sep 21 '22 04:09

Mike