Skip to content

Instantly share code, notes, and snippets.

@Ynng
Last active February 21, 2022 18:36
Show Gist options
  • Save Ynng/1a8182edd23f340248341735f509de24 to your computer and use it in GitHub Desktop.
Save Ynng/1a8182edd23f340248341735f509de24 to your computer and use it in GitHub Desktop.
Launch Windows Terminal at current directory with Ctrl+Alt+T
#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.
;; ----------------------------------------------------------------------------
;; Hotkey to use Ctrl+Alt+T to launch the Windows Terminal at the current directory.
;;
;; Link:
;; https://gist.github.com/Ynng/1a8182edd23f340248341735f509de24
;;
;; Credits:
;; https://www.autohotkey.com/boards/viewtopic.php?t=69925
;; https://blog.danskingdom.com/Bring-up-the-Windows-Terminal-in-a-keystroke/
;; ----------------------------------------------------------------------------
GetActiveExplorerPath()
{
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd == explorerHwnd)
{
return window.Document.Folder.Self.Path
}
}
}
}
SwitchToWindowsTerminal(curDir)
{
windowHandleId := WinExist("ahk_exe WindowsTerminal.exe")
windowExistsAlready := windowHandleId > 0
; If the Windows Terminal is already open, determine if we should put it in focus or minimize it.
if (windowExistsAlready = true)
{
activeWindowHandleId := WinExist("A")
windowIsAlreadyActive := activeWindowHandleId == windowHandleId
if (windowIsAlreadyActive)
{
; Minimize the window.
WinMinimize , "ahk_id %windowHandleId%"
} else
{
; Put the window in focus.
WinActivate , "ahk_id %windowHandleId%"
WinShow , "ahk_id %windowHandleId%"
}
}
; Else it's not already open, so launch it.
else
{
Run wt cmd /k cd %curDir%
}
}
^!t:: SwitchToWindowsTerminal(GetActiveExplorerPath())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment