Skip to content

Instantly share code, notes, and snippets.

@andrewgodwin
Created February 15, 2020 17:41
Show Gist options
  • Save andrewgodwin/89920ee02501ab12d09b02500897066c to your computer and use it in GitHub Desktop.
Save andrewgodwin/89920ee02501ab12d09b02500897066c to your computer and use it in GitHub Desktop.
AutoHotkey script for making Windows Terminal appear/disappear with a single keypress
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
F12::ToggleTerminal()
ShowAndPositionTerminal()
{
WinShow ahk_class CASCADIA_HOSTING_WINDOW_CLASS
WinActivate ahk_class CASCADIA_HOSTING_WINDOW_CLASS
SysGet, WorkArea, MonitorWorkArea
TerminalWidth := A_ScreenWidth * 0.9
if (A_ScreenWidth / A_ScreenHeight) > 1.5 {
TerminalWidth := A_ScreenWidth * 0.6
}
WinMove, ahk_class CASCADIA_HOSTING_WINDOW_CLASS,, (A_ScreenWidth - TerminalWidth) / 2, WorkAreaTop - 2, TerminalWidth, A_ScreenHeight * 0.5,
}
ToggleTerminal()
{
WinMatcher := "ahk_class CASCADIA_HOSTING_WINDOW_CLASS"
DetectHiddenWindows, On
if WinExist(WinMatcher)
; Window Exists
{
DetectHiddenWindows, Off
; Check if its hidden
if !WinExist(WinMatcher) || !WinActive(WinMatcher)
{
ShowAndPositionTerminal()
}
else if WinExist(WinMatcher)
{
; Script sees it without detecting hidden windows, so..
WinHide ahk_class CASCADIA_HOSTING_WINDOW_CLASS
Send !{Esc}
}
}
else
{
Run "c:\Users\Andrew\AppData\Local\Microsoft\WindowsApps\wt.exe"
Sleep, 1000
ShowAndPositionTerminal()
}
}
@pluveto
Copy link

pluveto commented Jul 13, 2021

Run "%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe"

@rbreaves
Copy link

couldn't get pluveto's to work either.. this did.

Run "C:\Users%A_UserName%\AppData\Local\Microsoft\WindowsApps\wt.exe"

Also I think removing A_ScreenHeight * 0.5, on line 18 makes more sense.. I have a mouse and sometimes I like to change my height and keep it bigger or smaller. Also I do appreciate how easy it is to set the width. I don't tend to need my mouse to change the width size.

@rbreaves
Copy link

rbreaves commented Dec 15, 2021

@andrewgodwin May I modify this slightly and include this in other MIT or GPLv2 projects? This will likely inspire me to do something similar on Linux as well.

@andrewgodwin
Copy link
Author

Sure, it's probably outdated now but do with it what you will!

@rbreaves
Copy link

Thanks, & well I tried a few other akh scripts that supposedly did this & they all failed lol, so you did something right 😂.

I’ve also knocked out how to better handle shared Tmux sessions. I’m very happy now that I can have multiple computers & monitors display the same session & tabs & it’s no longer resizing down to the smallest session but rather the largest.

Also thankfully Windows Terminal exists or I’d really be up a creek on Windows. The PTY support for UNIX/Linux is so poor in anything else besides mintty.

@jorpilo
Copy link

jorpilo commented Jan 27, 2022

Hey a couple modifications to make it work using env variables. Nice script! I slightly changed the Height and Width to make it more straight forward. Is there any way to get the current monitor where the active window is in? Right now it only shows on my first monitor.

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.

F12::ToggleTerminal()

ShowAndPositionTerminal()
{
    WinShow ahk_class CASCADIA_HOSTING_WINDOW_CLASS
    WinActivate ahk_class CASCADIA_HOSTING_WINDOW_CLASS

    SysGet, WorkArea, MonitorWorkArea
    TerminalWidth := A_ScreenWidth * 0.9
    TerminalHeight := A_ScreenHeight * 0.5

    WinMove, ahk_class CASCADIA_HOSTING_WINDOW_CLASS,, (A_ScreenWidth - TerminalWidth) / 2, WorkAreaTop - 2, TerminalWidth, TerminalHeight
}

ToggleTerminal()
{
    EnvGet, home, HOME 
    EnvGet, localappdata, LOCALAPPDATA 

    WinMatcher := "ahk_class CASCADIA_HOSTING_WINDOW_CLASS"

    DetectHiddenWindows, On

    if WinExist(WinMatcher)
        ; Window Exists
    {
        DetectHiddenWindows, Off

        ; Check if its hidden
        if !WinExist(WinMatcher) || !WinActive(WinMatcher)
        {
            ShowAndPositionTerminal()
        }
        else if WinExist(WinMatcher)
        {
            ; Script sees it without detecting hidden windows, so..
            WinHide ahk_class CASCADIA_HOSTING_WINDOW_CLASS
            Send !{Esc}
        }
    }
    else
    {
        Run "%localappdata%\Microsoft\WindowsApps\wt.exe", %home%
        Sleep, 1000
        ShowAndPositionTerminal()
    }
}

@miozus
Copy link

miozus commented Mar 24, 2022

        Run "wt.exe"

just simple one line which is enough to call Windows Terminal.

@lu0
Copy link

lu0 commented May 10, 2022

I've been using the script for a while, and made a few modifications in my forked gist to make it work on multiple workspaces / virtual desktops.

#NoTrayIcon
#SingleInstance Force

;
; This scripts toggles between states raised and hidden of a Windows Terminal,
; or opens a new one if not opened, using  `Super + T` or `Super + Enter`
; 
; Supports multiple workspaces or virtual desktops.
;

#T::
#Enter::
    ToggleTerminal()

ToggleTerminal() {
    matcher := "ahk_class CASCADIA_HOSTING_WINDOW_CLASS"
    DetectHiddenWindows, On
    if WinExist(matcher) {

        if !WinActive(matcher) {
            ; Hide it first to alow raising it later on a different workspace
            HideTerminal()
            ShowTerminal()
        } else if WinExist(matcher) {
            HideTerminal()
        }

    } else {
        OpenNewTerminal()
    }
}

OpenNewTerminal() {
    Run C:\Users\%A_UserName%\AppData\Local\Microsoft\WindowsApps\wt.exe
    Sleep, 1000
    ShowTerminal()
}

ShowTerminal() {
    WinShow ahk_class CASCADIA_HOSTING_WINDOW_CLASS
    WinActivate ahk_class CASCADIA_HOSTING_WINDOW_CLASS
}

HideTerminal() {
    WinHide ahk_class CASCADIA_HOSTING_WINDOW_CLASS
}

@tommdq
Copy link

tommdq commented Jul 17, 2023

Hi there! i'm using the same script but wt.exe is not opening at all. not even if i open it from the file explorer. Anyone with this issue?
If i open the terminal app from it's shortcut it opens without any problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment