Advertisement
slyfox1186

open-wsl-terminal-here.ahk

Dec 18th, 2022 (edited)
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*____________________________________________________________________________________
  2.     [ OpenTERMINALHere ]
  3.  
  4.     By: SlyFox1186
  5.     Pastebin: https://pastebin.com/u/slyfox1186
  6.     GitHub: https://github.com/slyfox1186/
  7.     - Looking for a coder? You found him. Contact me on GitHub.
  8.  
  9.     Purpose:
  10.     - This will open Windows' WSL terminal to the active file explorer folder.
  11.     - If necessary modify the below variable "_osName" to the wsl dist you have installed.
  12. */
  13.  
  14. !x::_OpenWSLHere()
  15.  
  16. _OpenWSLHere()
  17. {
  18.  
  19. _osName := "Ubuntu"
  20.  
  21.     ; OPEN CMD.EXE USING THE ACTIVE EXPLORER WINDOW'S FOLDER PATH
  22.     If winexist("ahk_class CabinetWClass ahk_exe explorer.exe")
  23.         _winHwnd := WinActive()
  24.     For win in ComObjCreate("Shell.Application").Windows
  25.         if (win.HWND = _winHwnd)
  26.         {
  27.             _pwd := SubStr(win.LocationURL, 9)
  28.             _pwd := RegExReplace(_pwd, "%20", " ")
  29.             _pwd := RegExReplace(_pwd, "%21", "!")
  30.             _pwd := RegExReplace(_pwd, "%23", "#")
  31.             _pwd := RegExReplace(_pwd, "%24", "$")
  32.             _pwd := RegExReplace(_pwd, "%25", "%")
  33.             _pwd := RegExReplace(_pwd, "%26", "&")
  34.             _pwd := RegExReplace(_pwd, "%27", "'")
  35.             _pwd := RegExReplace(_pwd, "%28", "(")
  36.             _pwd := RegExReplace(_pwd, "%29", ")")
  37.             _pwd := RegExReplace(_pwd, "%2D", "-")
  38.             _pwd := RegExReplace(_pwd, "%2E", ".")
  39.             _pwd := RegExReplace(_pwd, "%30", "*")
  40.             _pwd := RegExReplace(_pwd, "%3A", ":")
  41.             _pwd := RegExReplace(_pwd, "%3F", "?")
  42.             _pwd := RegExReplace(_pwd, "%40", "@")
  43.             _pwd := RegExReplace(_pwd, "%5B", "[")
  44.             _pwd := RegExReplace(_pwd, "%5D", "]")
  45.             _pwd := RegExReplace(_pwd, "%5E", "^")
  46.             _pwd := RegExReplace(_pwd, "%5F", "_")
  47.             _pwd := RegExReplace(_pwd, "%60", "``")
  48.             _pwd := RegExReplace(_pwd, "%7B", "{")
  49.             _pwd := RegExReplace(_pwd, "%7C", "|")
  50.             _pwd := RegExReplace(_pwd, "%7D", "}")
  51.             _pwd := RegExReplace(_pwd, "%7E", "~")
  52.             _pwd := RegExReplace(_pwd, "/", "\")
  53.         }
  54.     Run, "%A_windir%\System32\wsl.exe" -d %_osName% --cd "%_pwd%",, Max, _wPID
  55.     _winPID := "ahk_pid " . _wPID
  56.     WinWait, %_winPID%,, 2
  57.     WinActivate, %_winPID%
  58.     Sleep, 500
  59.     if !WinExist(_winPID)
  60.         MsgBox, Script Error:`r`rYou may need to change the variable '_osName' to the name of the distribution you have installed.`r`rExamples: Debian, Ubuntu, or other.`r`rRemember the explorer.exe window must be the ACTIVE window when you activate the hotkey!
  61. }
  62. return
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement