9

Is there any possibility to run Clink within Windows Terminal Preview?


I tried to add this entry in Settings:

        {
            "hidden": false,
            "name": "Clink",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "commandline": "\"C:\\Program Files (x86)\\clink\\0.4.9\\clink.bat\" startmenu --profile ~\\clink"
        }

but it opens Clink in a new window.

I think clink.bat must be somehow modified because it launches Clink with:

start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"

2 Answers2

13

Looking into the clink.bat file

Let us look at the clink.bat file:

:: Copyright (c) 2012 Martin Ridgers
:: License: http://opensource.org/licenses/MIT

@echo off

:: Mimic cmd.exe's behaviour when starting from the start menu.
if /i "%1"=="startmenu" (
    cd /d "%userprofile%"
    shift /1
)

:: Check for the --profile option.
if /i "%1"=="--profile" (
    set clink_profile_arg=--profile "%~2"
    shift /1
    shift /1
)

:: If the .bat is run without any arguments, then start a cmd.exe instance.
if "%1"=="" (
    call :launch
    goto :end
)

:: Pass through to appropriate loader.
if /i "%processor_architecture%"=="x86" (
        "%~dp0\clink_x86.exe" %*
) else if /i "%processor_architecture%"=="amd64" (
    if defined processor_architew6432 (
        "%~dp0\clink_x86.exe" %*
    ) else (
        "%~dp0\clink_x64.exe" %*
    )
)

:end
set clink_profile_arg=
goto :eof

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:launch
start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"
exit /b 0

This is quite well commented so we can see the following chronological structure:

  1. Move to the %userprofile% folder
  2. Set the clink_profile_arg to the value of PROFILE_DIR if the call is in the form clink.bat --profile PROFILE_DIR
  3. If there are no arguments jump to the launch code and then end (by jumping to the end of the file)
  4. Select the right .exe based on the system's architecture (technically the process architecture that calls this function: Possible values of %PROCESSOR_ARCHITECTURE%)
  5. The launch "definition" (technically labels)

You have correctly identified that the launch labelled code is what can be changed, let us look at it further:

start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"

So this runs the start command with some arguments, including the string "Clink" and what appears to be a cmd.exe with its own command line arguments. %~dpnx0 being: drive,path,name,extension, 0th argument (see, syntax-args), and %clink_profile_arg% the variable defined earlier.

Looking at start :

Starts a separate Command Prompt window to run a specified program or command.

The bold emphasis is my own, but we instantly now can see why you observed the behaviour you described.

We have several options to consider now.

Option 1 - New clink_terminal.bat based on clink.bat

Although we could edit clink.bat the better option would be to make a separate file that we use just for the Terminal.

We can simply change :launch to:

cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"

and then use your commandline: with clink_terminal.bat instead.

Option 2 - directly use clink with its command line arguments

Hopefully through you have seen that you can effectively replace calling the .bat and simply call clink directly with its arguments.

Here assuming you are using a x64 machine:

commandline: "cmd.exe /s /k "PATH_TO_CLINK\\clink_x64.exe inject --profile PROFILE_DIR""

Set a GUID !!!

All the profiles in Terminal have a GUID, you can easily generate one yourself.

Open a PowerShell window and run new-guid

PS C:\ANYWHERE> New-Guid

Guid
----
c97d08e9-03fc-491f-bbd7-9a12b9d6e191
Adehad
  • 519
  • 6
  • 16
1

Based on Adehad's answer, first generate new GUID: Open a PowerShell window and run New-Guid command:

PS C:\ANYWHERE> New-Guid
Guid
----
ed8f4d54-a8db-4628-ab4f-317cc6469b07 

Then in Windows Terminal, in Settings, select Open JSON file (the last option at the bottom left menu). It will open file from the path like C:\Users\your_user_name\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json . There find "profiles" section, and in "list" add something like this (use your newly generated GUID here):

        {
            "commandline": "cmd.exe /s /k \"C:\\my_programs\\clink_0.4.9\\clink_x64.exe inject --profile PROFILE_DIR\"",
            "guid": "{ed8f4d54-a8db-4628-ab4f-317cc6469b07}",
            "hidden": false,
            "name": "clink"
        }

Save the file, and if there are errors, Windows Terminal will show you the error message and location in the settings.json file where error happened. Usually it's related to not properly escaped quotes or slashes. Save changes, restart Windows Terminal, and see in options new item, clink.

lugger1
  • 1,843
  • 3
  • 22
  • 31