How to bring up last terminal window with a shortcut key?

I have a terminal pinned to my Unity launcher sidebar on position 10. This way I can press Super+0 to "click" the launcher icon which brings the latest terminal window to the top.

enter image description here

If having it in the launcher is ok for you (one of the first 10 positions, otherwise it won't get a shortcut!), this will work.


I use guake and I'm very happy with it. Press F12, a terminal window appears, press F12 again, it disappears but keeps running in the background. Also: looks really cool.


You can put the script below under a key combination. If you press the key combination, the terminal window(s) will disappear (completely). Press it again, they will pop up again exactly in the state as you had it.

Only thing you need to to (once) is to add the identifying string in your terminal's window name (the terminal window has the same name in most cases)

To use it

Install both xdotool and wmctrl:

sudo apt-get install xdotool
sudo apt-get install wmctrl
  1. Copy the script into an empty file, save it as hide_terminal.py
  2. In the head section, set the identifying string of the terminal window's name
  3. Run it under a key combination:

    python3 /path/to/hide_terminal.py
    

The script

#!/usr/bin/env python3
import subprocess
import os

home = os.environ["HOME"]
hidden_windowid = home+"/.window_id.txt"

get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
# --- set the identifying string in the terminal window's name below (you mentioned "Terminal"
window_idstring = "Special_window"
# ---
def execute(cmd):
    subprocess.check_call(cmd)

w_id = [l.split()[0] for l in get(["wmctrl", "-l"]).splitlines() if window_idstring in l]
if len(w_id) !=0:
    for w in w_id:
        execute(["xdotool", "windowunmap", w])
        with open(hidden_windowid, "a") as out:
            out.write(w+"\n")
else:
    try:
        with open(hidden_windowid) as read:
            for w in [w.strip() for w in read.readlines()]:
                try:
                    execute(["xdotool", "windowmap", w])
                except subprocess.CalledProcessError:
                    pass
        with open(hidden_windowid, "wt") as clear:
            clear.write("")
    except FileNotFoundError:
        pass