ZSH : Open terminal where you left, for each session

There is some snippets for ZSH configuration which allow you to re-open the session in the folder where it was last closed available on the web. The problem is that you often launch 3 sessions at the same time, work on them and then quit/reboot/loose SSH connections/… So you will re-log 3 sessions which will start in the same last opened folder.

I propose a version allowing to keep the last folder per-session. Each ZSH session receive a number and write the current folder in a per-session file. When you open a new session it opens the file number associated to the session number.


Add somewhere inĀ .zshrc :

mkdir -p ~/.cwd/
session_num=`pgrep zsh | wc -l`
function cd() {
builtin cd "$@";
echo "$PWD" > ~/.cwd/$session_num
echo "$PWD" > ~/.cwd/last
export cd
function cwd() {
if [ -e ~/.cwd/$session_num ] ; then
cd "$(cat ~/.cwd/$session_num)"
cd "$(cat ~/.cwd/last)"
echo "This is session #$session_num"

And at the bottom of the file :



Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.