Urxvt Open new tab with the same current directory as the current tab

rxvt

Whenever a new tab is opened in urxvt, the cwd(current working directory) is the default directory set in bashrc.

How to make the new tab to be opened at the cwd of the current tab?

Best Answer

First, you could override the cd builtin with a function that stores the current directory every time you change a directory.

cd() {
    command cd $@
    pwd > ~/.curdir
}

Then, you could change to this last known directory in every new shell you open:

command cd `cat ~/.curdir`

If you place both these snippets in your ~/.bashrc, every new shell you open with have the overridden cd command and will try to change to the last known directory:

#!/bin/bash

cd() {
    command cd $@
    pwd > ~/.curdir
}

command cd `cat ~/.curdir`

Note that I used the command builtin to gain access to the original cd command instead of the function named cd(). Also note that doing this will probably have unintended side effects, especially when you have multiple shells open at the same time (which is kind of the whole point of using urxvt tabs).

Related Question