Using screenrc, how can I make `C-a c` open a new window at the current window's working directory?

2

Using screenrc, how can I make C-a c open a new window at the current window's working directory? By default it appears to open the new window at the working directory at the time the original screen session is invoked.

Matt Joiner

Posted 2011-06-02T05:03:40.220

Reputation: 825

Answers

0

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.

jáquer

Posted 2011-06-02T05:03:40.220

Reputation: 825

1I read that answer, but thought stuff was a placeholder for something else. What does the ^M in your code sample above? – Matt Joiner – 2011-06-02T05:36:16.810

This has done it, but without the extra ^M, perhaps this was from copy-pasting? – Matt Joiner – 2011-06-02T05:37:33.187

7

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:

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

  2. screen -X chdir \$PWD tells screen to execute the command chdir, 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 with stuff is necessary.

  3. The screen command within an already running screen creates a new window just like C-a C-c.

  4. ^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 press enter (or C-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$

Matt Eckert

Posted 2011-06-02T05:03:40.220

Reputation: 334

Any ideas on how to make this work in a named screen session? http://superuser.com/questions/1038576/gnu-screen-open-new-window-in-working-directory-of-current-window-in-a-named @MattEckert

– Paul Caheny – 2016-02-11T15:35:52.287

Any Idea why the ^M isn't necessary? – Matt Joiner – 2011-06-02T09:21:21.083

1It is necessary, so that a carriage return is generated at the end of the stuff string. – Matt Eckert – 2011-06-02T15:41:41.283

0

Here's a copy of my own answer to a similar question on stackoverflow.com:

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"

stiemannkj1

Posted 2011-06-02T05:03:40.220

Reputation: 276