Ubuntu – Focus existing terminal with `Ctrl-Alt-T` shortcut

command linegnome-terminalwindow

I tend to use the terminal a lot,

So I wondering if there is a way I can make Ctrl+Alt+T focus the existing terminal if there is one, otherwise create a new terminal?

Best Answer

Create a small script which will raise the GNOME Terminal:

echo 'xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)'> ~/raiseterminal.sh && chmod +x ~/raiseterminal.sh

or if you want to check if Terminal is already running, use:

echo -e $'if ps aux | grep "[g]nome-terminal" > /dev/null\n then xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)\n else gnome-terminal &\nfi' > ~/raiseterminal.sh && chmod +x ~/raiseterminal.sh

This will create the script ~/raiseterminal.sh with this content:

if ps aux | grep "[g]nome-terminal" > /dev/null                                                                                                                                                                                
 then xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)                                                                                                                                            
 else gnome-terminal&                                                                                                                                                                                                          

Open the preferences to set up a custom keyboard shortcut and set the command to /home/$USER/raiseterminal.sh, but make sure to change $USER to your actual username.

If you only want to raise the terminal on a specific screen or desktop, see xdotool search --help for more information on how to do this.

There are also various other methods which work better with other window managers.