Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1403  / 2 Years ago, tue, august 30, 2022, 3:38:05

I find particularly useful on laptops to keep open (gnome-)terminals (and eventually other applications) when logging out / turning off; I think this was the default in some older Xubuntu version I had.
In particular I would like to keep open all terminal tabs in the same directory as in the previous session: is there a way to do this?



I have checked this:



http://www.howtogeek.com/203952/how-to-automatically-remember-running-applications-from-your-last-session-in-ubuntu-14.04/



but it seems it does not apply to paths in terminals.



I have Gnome/Unity on Ubuntu 14.04, but KDE has the same limit.


More From » unity

 Answers
4

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:




  1. 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.

  2. .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.


[#21623] Tuesday, August 30, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingsta

Total Points: 391
Total Questions: 103
Total Answers: 124

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
ingsta questions
Sun, Oct 23, 22, 01:42, 2 Years ago
Sat, Oct 30, 21, 11:27, 3 Years ago
Sun, Nov 28, 21, 12:49, 2 Years ago
;