Bash – Spawn new terminal window with the same directory as the previous window

awesomebashgnome-terminallinuxshell-script

I'm running Awesome WM on the latest Arch Linux with Gnome Terminal and the default bash. I've managed to get this working, but I wonder if any form of improvement is possible with this implementation. The question does NOT relate to opening new tabs, only to spawning new terminal windows with Awesome WM.

I have rewritten the "cd" command to save the current working directory in the ".cd_extend" file:

~/.bashrc

alias cd='source ~/.cd_extend'

~/.cd_extend

#!/bin/bash

command cd $1
echo $(pwd) > ~/.terminal_directory

When I spawn a new terminal, the ".terminal_directory" is read and appended as an argument to gnome terminal's "–working-directory" flag.

~/.dotfiles/open_terminal.sh

#!/bin/bash

DIR=$(cat ~/.terminal_directory)
gnome-terminal --working-directory=$DIR

awesomewm rc.lua

terminal   = "~/.dotfiles/open_terminal.sh"
awful.key({ modkey, }, "Return", function () awful.util.spawn(terminal) end)

I wonder if I have missed any internal bash functionality which could have simplified this and if there is room for improvement.

Best Answer

In the menu of Gnome-terminal, use:

File --> Open Terminal 

That will open a new window using the pwd as the directory.

Also, you may set the open tabs:

Edit --> Preferences --> General --> Open new terminals in: --> select tab.

So new terminals will open in the same window with the same pwd.
You will still be able to open new windows if needed:

Alt-F2 --> gnome-terminal

Related Question