11

I use terminal so often for making quick commands and then I leave it in the background so that I end up getting 20+ terminal sessions open while I'm working. This is because it's very quick to simply use the shortcut key and type a command.

Is there a way to set up the shortcut key so I bring my up last terminal window instead of creating a new one?

  • 1
    would an edited version of this one be a solution? It would be drastically simpeler to do it for a single terminal window and make it disappear completely. askubuntu.com/a/595244/72216 let me know, I can convert the answer easliy. What is the (window) name of your terminal window? – Jacob Vlijm Mar 19 '15 at 14:59
  • 2
    Try this – A.B. Mar 19 '15 at 15:09
  • The window name I use is simply "Terminal". But would the script method you have there still work if I use tabs in the terminal? (i.e. ctrl+shift+T) I rename them as well to help identify which computer I am sshed into. – Klik Mar 19 '15 at 15:10
  • As long as the string "terminal" is there it would. Is thjat the case? – Jacob Vlijm Mar 19 '15 at 15:13
  • @JacobVlijm It's not the case. I often change the name to "ssh" or "Local" depending which on what I'm doing. I could modify my title changing script to include a unique prefix though. I.e. '%.%' or something like that. – Klik Mar 20 '15 at 18:29
13

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.

  • Oh that's a great idea! I like this answer, but I'm going to wait to see what other people come up with. I was thinking of making a script using xdotool to find the terminal and bring it to the front. – Klik Mar 19 '15 at 15:04
  • I think this solution is the most easily implementable and serves exactly the purpose I'm looking for. For that reason I will mark this as the answer. – Klik Mar 20 '15 at 18:35
10

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.

  • Another vote for guake here. Worth noting that if you use multiple (different size) monitors, some versions need a fix to make it work properly - still worth it though. – Holloway Mar 20 '15 at 9:18
  • Became so dependent on guake that when I switched to xmonad I emulated it in that. Must have! – Tony Martin Mar 25 '15 at 2:35
6

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
  • 1
    You have a script for everything, don't you? ;D – Byte Commander Mar 19 '15 at 16:08
  • 1
    @ByteCommander I can't resist, it's stronger than me :) – Jacob Vlijm Mar 19 '15 at 16:09
  • Note that you also need to change the terminal's name in the last line if you're not using gnome-terminal. Also, it breaks if you have more than one open terminal. On my system, 1st run hides the active one, 2nd run hides the second one and 3rd run only returns the 2nd terminal. The 1st is lost for ever. – terdon Mar 19 '15 at 17:36
  • @terdon Arrgh, You're right! Will fix it and make it hide/show all terminal windows. – Jacob Vlijm Mar 19 '15 at 17:40
  • More to the point, why not just do this in bash? Why would you bring python into it if all you're doing with python is launching system calls? – terdon Mar 19 '15 at 17:42
5

This is the same thing as Jacob Vlijm's answer, just written in bash:

#!/usr/bin/env bash

## window_name will be the first argument passed or, if no
## argument was given, "Terminal"
window_name=${1:-"Terminal"}

## Get the list of open terminals
terms=( $(wmctrl -l | grep "$window_name" | cut -d ' ' -f 1) )

## If all terminals are hidden
if [ -z "${terms[0]}" ]
then
    ## Read the IDs of hidden windows from .hidden_window_id
    while read termid
    do
        xdotool windowmap "$termid"
    done < ~/.hidden_window_id
## If there are visible terminals
else
    ## Clear the .hidden_window_id file
    > ~/.hidden_window_id
    ## For each open terminal
    for i in "${terms[@]}"
    do
        ## Save the current ID into the file
        printf "%s\n" "$i" >> ~/.hidden_window_id
        ## Hide the window
        xdotool windowunmap "$i"
    done
fi

If you save it as ~/bin/show_hide.sh, you can run it by giving the identifying string of any window you want to hide. If no string is given, it will work on Terminal:

show_hide.sh Terminal
1

I'm using gnome-shell with 'Drop Down Terminal' extension, default shortcut is TAB but it's easily changed.

1

This simple wmctrl command will raise a window with a given string in the title or if no window exists containing the string, run a command.

wmctrl -a <str> || <command to launch application>

eg for gedit I can use

wmctrl -a gedit || gedit

To find a suitable string for your application window, open your application and run

wmctrl -l
0

the following approach worked for me:

#!/usr/bin/env bash

# Written by Eric Zhiqiang Ma (http://www.ericzma.com)
# Last update: Jul. 9, 2014

# Read the tutorials at
# http://www.systutorials.com/5475/turning-gnome-terminal-to-a-pop-up-terminal/

# Required tools: xdotool

terminal="gnome-terminal"
stat_file="/dev/shm/actiavte-termianl.term.$USER"
termtype="Terminal"
wait_sec=1
max_wait_cnt=4

# parse options first
if [ "$1" != "" ]; then
    terminal="$1"
fi


term_exists () {
    allterms=`xdotool search --class "$termtype"`
    for e in $allterms; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

create_terminal () {
    echo "Create new terminal"
    $terminal --maximize &

    exists=1
    wait_cnt=0
    until [ "$exists" == "0" ]; do
        # sleep a while
        sleep $wait_sec

        # Note that the focus window may be from a terminal that
        # already exists; the makes create_terminal choose the existing terminal
        # Making the wait_sec large enough so that the terminal can be created and
        # displayed can make the false choosing more unlikely.

        term=$(xdotool getwindowfocus)
        term_exists "$term"
        exists=$?
        # avoid infinite wait
        let wait_cnt=wait_cnt+1
        if [ $wait_cnt -gt $max_wait_cnt ]; then
            echo "Wait for too long. Give up."
            term=""
            exists=0
        fi
    done

    echo "Created terminal window $term"
    # save the state
    echo "$term" >$stat_file
}

# read the state
if [ -f $stat_file ]; then
    term=$(cat $stat_file)
fi

# check whether it exists
term_exists "$term"
exists=$?
if [[ "$exists" != "0" ]]; then
    create_terminal
    exit 0
fi

# check whether it is already activated
curwin=$(xdotool getwindowfocus)

if [ "$term" == "$curwin" ]; then
    # deactivate (minimize) the terminal if it is currently activated
    xdotool windowminimize $curwin
else
    # activate the terminal if it is not currently activated
    xdotool windowactivate $term
fi

exit 0

then just give it execute permissions and bind the script to a key in settings.

source:

http://www.systutorials.com/5475/turning-gnome-terminal-to-a-pop-up-terminal/

  • 1
    Add some important parts from that link in case the link breaks or changes. – George Udosen Jan 20 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.