Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
1  
I read that answer, but thought stuff was a placeholder for something else. What does the ^M in your code sample above? –  Matt Joiner Jun 2 '11 at 5:36
    
This has done it, but without the extra ^M, perhaps this was from copy-pasting? –  Matt Joiner Jun 2 '11 at 5:37

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$
share|improve this answer
    
Any Idea why the ^M isn't necessary? –  Matt Joiner Jun 2 '11 at 9:21
1  
It is necessary, so that a carriage return is generated at the end of the stuff string. –  Matt Eckert Jun 2 '11 at 15:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.