Using screenrc, how can I make `C-a c` open a new window at the current window's working directory?
Based on this SO answer, I think this should work:
bind c stuff "screen -X chdir \$PWD; screen^M"
I'll go try it on my remote shell and report back if it works for me.
edit: Yup, it works. The first "bind" command is actually not needed.
By default, screen
also binds C-a C-c
to create a new window, so you might want to add another line to your .screenrc
to handle this case:
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"
Clarification about how this command works:
-
stuff
puts its argument string directly into the current window:Command:
stuff string
Stuff the string
string
in the input buffer of the current window. screen -X chdir \$PWD
tells screen to execute the commandchdir
, which changes its operational directory (where new screen windows will start) to the environment variable$PWD
, which contains the current working directory. This is impossible to do within.screenrc
alone; therefore, manipulating the input buffer withstuff
is necessary.The
screen
command within an already runningscreen
creates a new window just likeC-a C-c
.^M
generates a carriage return, which tells the shell to execute the command which is now in the buffer. Without it, you would have to pressenter
(orC-m
, of course).
Consequently, this bind will leave cruft like this in the window you execute it in:
user@host:~/directory$ screen -X chdir $PWD;screen
user@host:~/directory$