gnome-terminal has a great feature where opening a new tab or window starts the new shell with cwd = the cwd of the previously-focussed window.
I usually run GNU screen in one of my gnome-terminal tabs. Partly for the scrollback, partly for the compact and enumarated display of 5 to 10 windows, partly for the quick keystrokes to switch to a specific or the previous tab.
But I've been starting to wish I could cd in one window to the CWD of another window. The question is, how?
(BTW, is a stackexchange Q&self-A an appropriate way to share a neat alias, shell function, or hack like this that I came up with? I don't have a block, and I don't think twitter or facebook would be good options.)
Best Answer
This method is portable to any Unix system, but depends on the functionality of GNU bash's
cd -P
to make it not ugly.Put this (or the other version below, that preserves logical directories) in your
~/.bashrc
(or.zshrc
or whatever), so it runs for every interactive shell (inside and outside of screen):So you can open a new screen window and
cds 5
will take you to the cwd of the shell in window 5.This even works for shells started OUTSIDE of screen, and even before your screen session existed. (since in that case, the glob expansion happens at runtime of
cds
, rather than when it was defined like with the array-variable hack that I commented out, since it's worse in every way.)Total overhead:
Without
cd -P
, the\w
in your$PS1
would expand to/proc/3069/cwd
, rather than the canonical (symlinks followed) path you get with-P
.The version that uses PROMPT_COMMAND could modify
cds()
to cd to take you to the logical directory. (Store CWD as text in a file, instead of in a symlink.pwd > "$CDS_DIR/$WINDOW"
is just a shell builtin, so less overhead than fork/execing a binary. Also saves the trouble of working around missing GNUreadlink
on some non-Linux systems.) This would be useful if you often work in symlinks to directories, wherepwd -P
(and/bin/pwd
) isn't the same aspwd
.You could override the
cd
,pushd
, andpopd
builtins with functions that update the symlink, instead of doing it EVERY prompt. With that setup, interactive use of things like(cd foo; command there)
will fool your setup, because thecd
that runs in the subshell will update the symlink, but without modifying the pwd of the main shell process.Ok, here's the "other version", that hooks
cd
,pushd
, andpopd
, and will take you to the same logical directory as your shell in whatever screen window:Overhead: an
open(2)
andwrite(2)
to tmpfs for everycd
,pushd
, andpopd
you type or paste in. Otherwise the same. Still not going to hurt, on any system powerful enough to runbash
andscreen
in the first place. :)Credit to https://superuser.com/a/54161/20798 for the idea of hooking cd. I like that a LOT better than
PROMPT_COMMAND
. Found that while looking to see if anyone else had invented this, and whether I should post here or on https://unix.stackexchange.com/.I used some bash-isms (like
[[ ]]
) because I think zsh supports them too, and hopefully nobody is using POSIX sh as their interactive shell.