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?
8 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 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.

- 30,738
- 21
- 105
- 131

- 18,694
- 7
- 33
- 32
-
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 Oct 10 '09 at 15:11
-
@DanMidwood The first part of your answer is for `.screenrc` right? The second part for the command line? – dlamblin Aug 10 '12 at 17:17
-
@dlamblin The first part of my answer is just setup for the remainder, those commands don't need to be put anywhere. But, to answer your question, they are in the format that would go into .screenrc – Dan Midwood Aug 11 '12 at 09:09
-
1@dlamblin If you're looking to do this I'd suggest taking Mike's answer, it does the same but in neater fashion. http://stackoverflow.com/a/3282023/119768 – Dan Midwood Aug 11 '12 at 09:12
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.

- 30,738
- 21
- 105
- 131

- 733
- 1
- 7
- 10
-
2If you're going to use 'stuff', you don't need the 'chdir': bindkey ^x stuff "screen^M" (with literal '^M' there) does what's necessary. The only drawback is that it leaves an extra command in the shell of whatever window you're in when you use it, but that's a small price to pay, I guess. I'm using this solution myself, now. – Joseph Jun 30 '11 at 20:02
-
2Mike's solution does not seem to work when the current open window is running vim. Any ideas would be welcome. – Covi Feb 12 '16 at 08:00
-
I didn't find any solution that would work when you already had a process running in a window, so I came up with my own idea. I added following lines to my .bash_profile file:
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.

- 30,738
- 21
- 105
- 131

- 101
- 1
- 3
-
1This answer is the best one which just works. After putting this to my .bashrc, every new screen window opens in a new dir. Thanks. I just don't know why it has not received many votes compared to the complicated not fully working accepted answer. – denysonique Dec 14 '12 at 23:51
-
1
Perhaps this is specific to Byobu, but simply typing screen
opens a new window in the current directory.

- 30,738
- 21
- 105
- 131

- 1,442
- 15
- 12
To make Screen open a new tab/window in the current directory, you can add the following code to your .screenrc
file:
bind c stuff "screen bash^M"
This will cause the Ctrl + a c command to open new tabs/windows in the directory of the current window/tab.
Note: You must ensure that Screen does not start a login shell by default, because that will cause the shell start in the default directory for a login shell rather than the current directory. This means that in your .screenrc
file, your shell
command cannot include a dash ('-') character.
For example, this is wrong (i.e., it will start a login shell):
shell -$SHELL
But this is right (i.e., it will not start a login shell):
shell $SHELL
Note 2: Unfortunately, this method does not behave exactly like the default new window/tab command in Screen. Instead, it writes the command to the current window and executes it to create the new window/tab, so it will not work during some long running shell process. In other words, this keyboard shortcut can only be executed whenever normal shell commands can be executed.
Note 3: If you want Screen to open new windows/tabs in the current directory and open a login shell, you can add the following code to your .screenrc
file:
bind c stuff "screen bash -l^M"

- 30,738
- 21
- 105
- 131

- 4,418
- 3
- 25
- 45
You could also run:
screen -X eval "chdir $(pwd)"
Or if you want to start a new window as soon as you set chdir, use:
screen -X eval "chdir $(pwd)" screen

- 81
- 1
- 5
I have a nearly perfect solution for Bash. :)
If you never use
password
to set a lockscreen password, just add this to file$HOME/.bash_profile
:export PROMPT_COMMAND='screen -p $WINDOW -X chdir "$PWD"'
Do you need a password? With this:
# The digest of password "abc" is ID1wIq4l2t7s6 export PROMPT_COMMAND='screen -p $WINDOW -X eval "password none" "chdir \"$PWD\"" "idle 0 password ID1wIq4l2t7s6"'
I just hope the developers of Screen add the environment variable PWD
as soon as possible.

- 30,738
- 21
- 105
- 131

- 135
- 1
- 7
In your .screenrc
file, add a line that uses the chdir
command if you want the same one every time.
If you have a running Screen session inside that session, you can type:
screen -X chdir [argument]
Without an argument it will be your home directory, the same result as typing cd
.
If you have a script (this is a programming Q&A site) or are outside Screen and Screen is running, you can issue:
`which screen` -x -X chdir [argument]
Which you'll likely follow with running some new process in Screen with:
`which screen` -x -X screen [command to run in that directory] [arguments for the command]

- 30,738
- 21
- 105
- 131

- 43,965
- 20
- 101
- 140