trapping EXIT status
Having learned a few tricks over the years, I've come up with another solution which might be a little more sane than the original idea that you can still find below. According to Signal Trap article on Gregg's Wiki, "In a bash or ksh (some implementations only) script, an EXIT trap is run on any exit, signalled or not." So detecting and handling exit via Ctrl+d , exit
command, kill <shell_pid>
is possible via trap "command" EXIT
method. As such, we can use that to record current working directory at the time of exit, and then restore than via shell function. The code below does exactly that. Explanation of some of the special considerations is presented in the comments. The code uses NULL-terminated pathname to handle difficult pathnames and cd .
for checking unlinked directories (in fact there is more than one use for cd .
, see related question Does cd . have use?), and verification if the recorded directory still exists to navigate to it.
# This part will be triggered when shell exits.
# cd . is necessary as check for unlinked or renamed directories
# we have to avoid printf "$(pwd)\0" because process substitution spawns subshell
# with cwd set to user's home, which makes saving current working directory pointless
# We could use `/bin/pwd`, but just in case system is non-POSIX compliant and doesn't have
# it we will just stick with shell built-in
trap "cd . && pwd > ~/.last_dir && printf '\0' >> ~/.last_dir" EXIT
# This part is for when shell starts
goback(){
IFS= read -r lastdir < ~/.last_dir
if [ -d "$lastdir" ]; then
cd "$lastdir"
fi
}
# Check if we've read ~/.bashrc in this session already
# just in case user runs `source ~/.bashrc`
if [ -z "$bashrc_read" ]; then
bashrc_read=1
export bashrc_read
goback
fi
What this still doesn't handle is tabs closed in QTerminal or closed windows.
Original answer
TL;DR: create a function that saves output of the pwd
command to a file, and then use that file to launch gnome-terminal
with whatever was in that file as argument to gnome-terminal --working-directory=
.
Step 1: getting your last directory:
Bellow is a function savewd
(for save working directory), which you should place at the end of your /home/username/.bashrc
or whatever rc file for whatever shell you are using.
function savewd ()
{
echo $(pwd) > ~/.lastdir
}
Next, place a call to this function inside your PS1 prompt, again - in your .bashrc file. For example, here is mine:
# Backslash is necessary. See
# https://askubuntu.com/questions/651871/why-is-my-function-not-re-evaluated-in-ps1
PS1=' \$(savewd) serg@ubuntu [\$(pwd)]
================================
$ '
Placing a function into your prompt executes the function every time you run something in the shell, even simply pressing enter. Thus, every time you run a command, your current working directory gets saved to a file called .lastdir
in your home folder.
NOTES:
- this will save the last working directory of your last open shell. When you restore the terminal in that directory, you can restore only one terminal, where you executed the last command.
.lastdir
has leading dot, hence it's a hidden file, which you cannot see unless you do ls -a
or enable viewing hidden files in your file manager
Step 2: create a restoring script
Open your favourite text editor and create the following script below:
#!/bin/sh
# set -x
LASTDIR="$(cat /home/username/.lastdir)"
gnome-terminal --working-directory=$LASTDIR &
Now you have a script that reads whatever was saved in the .lastdir
file, and opens a terminal with contents of .lastdir
as working directory.
NOTE: don't forget to make this script executable with chmod +x /path/to/script
. Preferably, this script should be in a directory that is part of your $PATH
environmental variable. Learn more about it.
Step 3: create a startup item
Now you can use that script to launch every time you log in. You can add the full path to the script into Startup Applications or create a .desktop
entry in your /home/username/.config/autostart
folder. For instance, I'd personally create a file /home/username/.config/autostart/gnome-term-restored.desktop
with the following contents:
[Desktop Entry]
Type=Application
Exec=/path/to/my/script.sh
Hidden=false
NoDisplay=false
Name=gnome-terminal-restored
Now every time you log back in, you will have this .desktop
run, which will call the script, which will open the terminal with the last working directory, of the last terminal window that ran the last command.
You can also add the full path to the script in step 2 as a shortcut, in Settings -> Keyboard -> Shortcuts -> Custom -> + button
.
Step 4
Enjoy it :)
NOTES: This will work with logging in and out. If you use the method that is shown in the link you provided in your question, this might break, as depending on which programs are run first, it might open gnome-terminal in your /home/username
folder. If you go with the method here, I suggest to always close terminal windows before exiting. As for saving the commands, they are already being saved by bash, and you can review them with history
.
Another idea is to edit /usr/share/applications/gnome-terminal.desktop
to execute gnome-terminal --working-directory=$(cat /home/yourusername/.lastdir)
or the script I wrote , but I do not recommend it.
IMHO, the way that Fabby suggests(i.e., hibernating) is better.
sudo pm-hibernate
and see if it works correctly. Your Swap partition should be at least as large as your RAM. – user68186 May 05 '15 at 15:55--save-config
option in Gnome terminal (which offered a workaround: https://askubuntu.com/questions/310705/some-fast-way-to-save-and-restore-tabs-of-terminal) has disappeared. – gluuke Feb 21 '19 at 11:22