April 30, 2016 Stefano Terna

Open multiple terminals at predefined positions, with different histories, from a Gnome icon

I was working on a project where I needed to open every time the same four terminals at different paths and I wanted to have command histories saved separately for each bash.
I ended up with this solution some times ago on Ubuntu 14.04. Now, before moving to 16.04, I’d like to have the procedure saved for later reference. So here you are! As the title suggest, this post is about:

  • Opening multiple terminals at predefined monitor positions
  • with different bash histories
  • from a Gnome desktop icon

The procedure relies on gnome-terminal. I use it on Ubuntu with Gnome. As far as I know Unity uses gnome-terminal as well, so the same should apply.

Setup

Each bash will have a different working directory.
We are going to use working dirs as ‘unique identifiers’ of each bash. (This could be improved using some env var with a uuid to identify each bash).
Throughout the post:

  • project directory: MyProject
  • workdir of bash X: MyProject/dirX, with X from 1 to 4

Of course each dir name can be customized.

Now, prepare the directories (the example assumes MyProject in the home dir, but it can be anywhere):

cd ~ && mkdir MyProject && mkdir MyProject/dir1 && mkdir MyProject/dir2 && mkdir MyProject/dir3 && mkdir MyProject/dir4  && cd MyProject/

Opening multiple terminals at predefined monitor positions

For this task We use the gnome-terminal option to save terminal configurations:

  • open 4 terminals (be sure you don’t have any other terminal opened)
  • in each of them
    cd MyProject/dirX
  • drag and resize each terminal at your preferred position/size
  • in one of the terminal save the configuration for ALL of them (this include workdir, position and size):
    gnome-terminal --save-config=../myproject-term-conf

multiple-bashes-with-different-histories

To open the saved configuration:

  • close all the terminals
  • open a new terminal and create a shell script:
    nano MyProject/open-terms.sh
  • paste this command into the script and save/exit:
    cd $HOME/MyProject
    gnome-terminal --load-config=myproject-term-conf
    
  • give execute permission:
    chmod +x MyProject/open-terms.sh
  • test it:
    MyProject/open-terms.sh

    – you should see the four terminals opening at correct positions with the desired working dirs.

Different bash histories

For this task we are going to add a snippet at the end of .bashrc:

  • edit .bashrc:
    cd ~ && nano .bashrc
  • add this snippet at the end of the file:
    TERMLIST="MyProject.dir1 MyProject.dir2 MyProject.dir3 MyProject.dir4"
    
    CURTERM=$(basename ${PWD%$(basename $PWD)}).$(basename $PWD)
    
    if [[ $TERMLIST =~ $CURTERM ]]
    then
        [[ -d ~/.history ]] || mkdir --mode=0700 ~/.history
        [[ -d ~/.history ]] && chmod 0700 ~/.history
        HISTFILE=~/.history/history.$CURTERM
        # set HISTFILESIZE to a large value
        HISTFILESIZE=4096
        HISTSIZE=4096
        ls -la
    fi
    

What the snippet does:

  • $TERMLIST contains the list of “terminals” for which you want to separately save histories
  • $CURTERM identify the currently opened terminal. Each terminal is identified as listed in $TERMLIST – slashes are substituted with dots because $CURTERM is used as the file name for the current history. Note that $PWD corresponds with the WORKDIR configured in myproject-term-conf
  • a .history directory is created in your home if it doesn’t exist
  • the bash env variable HISTFILE is set with the customized value for the current terminal
  • I also like to have a large history and WORKDIR content listed when the terminal opens

Test what we have done up to now:

  • Close any terminal
  • Open a new one and
    MyProject/open-terms.sh
  • The four terminals should open in correct positions
  • Now type a different command in each terminal, for example:
    echo X

    with X from 1 to 4

  • Now close all the terminals and open a new one, typing:
    ls .history/
  • You should see four history files, one for each terminal (keep in mind the first time you won’t see the history files until you have closed the terminals)
  • Now again
    MyProject/open-terms.sh
  • Try searching the previous echo commands in different histories!

Setup the Gnome desktop icon

  • Create Gnome desktop launcher:
    nano ~/.local/share/applications/open-terms.desktop
  • Paste the following (changing what needed)
    [Desktop Entry]
    Type=Application
    Terminal=false
    Exec=[full path to MyProject dir; don't use ~ or $HOME]/MyProject/open-terms.sh
    Name=Multi Terminals
    Comment=Multi Terminals Opener
    Icon=[your favorite icon here]
    
  • From now when searching Gnome for the Application “Multi Terminal”, a launcher should appear with your new icon
  • Right click on it and “add to Favorites” to add it to Gnome side bar.