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.


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):
[code language=”bash”]cd ~ && mkdir MyProject && mkdir MyProject/dir1 && mkdir MyProject/dir2 && mkdir MyProject/dir3 && mkdir MyProject/dir4 && cd MyProject/[/code]

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 [code language=”bash”]cd MyProject/dirX[/code]
  • 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): [code language=”bash”]gnome-terminal –save-config=../myproject-term-conf[/code]


To open the saved configuration:

  • close all the terminals
  • open a new terminal and create a shell script: [code language=”bash”]nano MyProject/open-terms.sh[/code]
  • paste this command into the script and save/exit:
    [code language=”bash”]
    cd $HOME/MyProject
    gnome-terminal –load-config=myproject-term-conf
  • give execute permission: [code language=”bash”]chmod +x MyProject/open-terms.sh[/code]
  • test it: [code language=”bash”]MyProject/open-terms.sh[/code] – 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: [code language=”bash”]cd ~ && nano .bashrc[/code]
  • add this snippet at the end of the file:
    [code language=”bash”]
    TERMLIST="MyProject.dir1 MyProject.dir2 MyProject.dir3 MyProject.dir4"

    CURTERM=$(basename ${PWD%$(basename $PWD)}).$(basename $PWD)

    if [[ $TERMLIST =~ $CURTERM ]]
    [[ -d ~/.history ]] || mkdir –mode=0700 ~/.history
    [[ -d ~/.history ]] && chmod 0700 ~/.history
    # set HISTFILESIZE to a large value
    ls -la

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 [code language=”bash”]MyProject/open-terms.sh[/code]
  • The four terminals should open in correct positions
  • Now type a different command in each terminal, for example: [code language=”bash”]echo X[/code] with X from 1 to 4
  • Now close all the terminals and open a new one, typing: [code language=”bash”]ls .history/[/code]
  • 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 [code language=”bash”]MyProject/open-terms.sh[/code]
  • Try searching the previous echo commands in different histories!

Setup the Gnome desktop icon

  • Create Gnome desktop launcher: [code language=”bash”]nano ~/.local/share/applications/open-terms.desktop[/code]
  • Paste the following (changing what needed)
    [Desktop Entry]
    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.