Jump to content
Microsoft Windows Bulletin Board

Shell integration in the Windows Terminal


Recommended Posts

Guest Mike Griese
Posted

Starting in Terminal 1.15 Preview, the Windows Terminal has started experimentally supporting some new “shell integration” features that make the command line easier to use. In earlier releases, we enabled the shell to inform the Terminal about the current working directory. Now, we have added support for more sequences that allow your shell to semantically describe parts of the terminal output as a “prompt,” a “command,” or “output.” The shell can also indicate whether a command has succeeded or failed.

 

This is a guide to some of the shell integration features we’ve rolled out as of Terminal v1.18. We’re planning on building even more features on top of these in the future, so we’d love to get some additional feedback on how folks using them.

 

 

Note: Notably, “marks” are still experimental, and are only enabled for Preview builds of the Terminal. They’ll break pretty regularly if you resize the Terminal frequently and will likely work unexpectedly when clearing the screen with [iCODE]clear[/iCODE].

 

 

Here’s a number of features that this makes possible today:

 

[HEADING=2]Open new tabs in the same working directory[/HEADING]

 

duplicate-tab-same-cwd.gif

 

[HEADING=2]Show marks for each command in the scrollbar[/HEADING]

 

164290075-a9f0b92c-3dde-4ce3-88cf-da5e451fe46c.thumb.gif.d7d0e9144c7e22b22b19a4add0117820.gif

 

[HEADING=2]Automatically jump between commands[/HEADING]

 

164290677-ffaafe09-81c4-4181-a4b8-db79a8aed235.thumb.gif.e456ae10ec46d6e737dd65322603b176.gif

 

[HEADING=2]Select the entire output of a command[/HEADING]

 

207696859-a227abe2-ccd4-4b81-8a2c-8a22219cd0dd.thumb.gif.1802bd3a0061463724ccd205535c13bf.gif

 

222840120-7a2493b2-2264-4e94-af2b-17bfacc90353.gif.b8a8407d06097e33b567746c94414090.gif

 

[HEADING=1]How does this work?[/HEADING]

 

Shell integration works by having the shell (or any command line application) write special “escape sequences” to the Terminal. These escape sequences aren’t printed to the Terminal – instead, they provide bits of metadata the terminal can use to know more about what’s going on in the application. By sticking these sequences into your shell’s prompt, you can have the shell continuously provide info to the terminal that only the shell knows.

 

For the following sequences:

 

  • [iCODE]OSC[/iCODE] is the string [iCODE]"\x1b]"[/iCODE] – an escape character, followed by [iCODE]][/iCODE]
  • [iCODE]ST[/iCODE] is the “string terminator”, and can be either [iCODE]\x1b\[/iCODE] (an ESC character, followed by [iCODE]\[/iCODE]) or [iCODE]\x7[/iCODE] (the BEL character)
  • Spaces are merely illustrative.
  • Strings in [iCODE]<>[/iCODE] are parameters that should be replaced by some other value.

 

The relevant supported shell integration sequences as of Terminal v1.18 are:

 

  • [iCODE]OSC 9 ; 9 ; <CWD> ST[/iCODE] (“ConEmu Set working directory“) – Tell the Terminal what the current working directory is. [iCODE]CWD[/iCODE] needs to be a Windows filesystem path for this to work. If using this in WSL or cygwin, you’ll need to use [iCODE]wslpath[/iCODE] or [iCODE]cygpath[/iCODE].
  • [iCODE]OSC 133 ; A ST[/iCODE] (“FTCS_PROMPT“) – The start of a prompt.
  • [iCODE]OSC 133 ; B ST[/iCODE] (“FTCS_COMMAND_START“) – The start of a command-line (READ: the end of the prompt).
  • [iCODE]OSC 133 ; C ST[/iCODE] (“FTCS_COMMAND_EXECUTED“) – The start of the command output / the end of the command-line.
  • [iCODE]OSC 133 ; D ; <ExitCode> ST[/iCODE] (“FTCS_COMMAND_FINISHED“) – the end of a command. [iCODE]ExitCode[/iCODE] If [iCODE]ExitCode[/iCODE] is provided, then the Terminal will treat [iCODE]0[/iCODE] as “success” and anything else as an error. If omitted, the terminal will just leave the mark the default color.

[HEADING=1]How to enable shell integration marks[/HEADING]

 

Supporting these features requires cooperation between your shell and the Terminal. You’ll need to both enable settings in the Terminal to use these new features, as well as modify your shell’s prompt.

 

To enable these features in the Terminal, you’ll want to add the following to your settings:

 

"profiles":
{
   "defaults":
   {
       // Marks in general
       "experimental.showMarksOnScrollbar": true,

       // Needed for both pwsh and CMD shell integration
       "experimental.autoMarkPrompts": true,

       // Add support for a right-click context menu
       // You can also just bind the `showContextMenu` action
       "experimental.rightClickContextMenu": true,
   },
}
"actions":
[
   { "keys": "ctrl+up",   "command": { "action": "scrollToMark", "direction": "previous" }, },
   { "keys": "ctrl+down", "command": { "action": "scrollToMark", "direction": "next" }, },

   // Add the ability to select a whole command (or its output)
   { "keys": "ctrl+shift+up",   "command": { "action": "selectOutput", "direction": "prev" }, },
   { "keys": "ctrl+shift+down", "command": { "action": "selectOutput", "direction": "next" }, },

   { "keys": "ctrl+alt+shift+up",   "command": { "action": "selectCommand", "direction": "prev" }, },
   { "keys": "ctrl+alt+shift+down", "command": { "action": "selectCommand", "direction": "next" }, },
]

 

How you enable these marks in your shell varies from shell to shell. Below are tutorials for CMD and PowerShell.

 

[HEADING=2]PowerShell ([iCODE]pwsh.exe[/iCODE])[/HEADING]

 

If you’ve never changed your PowerShell prompt before, you should check out about_Prompts first.

 

We’ll need to edit your [iCODE]prompt[/iCODE] to make sure we tell the Terminal about the CWD, and mark up the prompt with the appropriate marks. PowerShell also lets us include the error code from the previous command in the [iCODE]133;D[/iCODE] sequence, which will let the terminal automatically colorize the mark based if the command succeeded or failed.

 

Add the following to your PowerShell profile:

 

$Global:__LastHistoryId = -1

function Global:__Terminal-Get-LastExitCode {
 if ($? -eq $True) {
   return 0
 }
 if ("$LastExitCode" -ne "") { return $LastExitCode }
 return -1
}

function prompt {

 # First, emit a mark for the _end_ of the previous command.

 $gle = $(__Terminal-Get-LastExitCode);
 $LastHistoryEntry = $(Get-History -Count 1)
 # Skip finishing the command if the first command has not yet started
 if ($Global:__LastHistoryId -ne -1) {
   if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
     # Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
     $out += "`e]133;D`a"
   } else {
     $out += "`e]133;D;$gle`a"
   }
 }


 $loc = $($executionContext.SessionState.Path.CurrentLocation);

 # Prompt started
 $out += "`e]133;A`a";

 # CWD
 $out += "`e]9;9;`"$loc`"`a";

 # (your prompt here)
 $out += "PWSH $loc$('>' * ($nestedPromptLevel + 1)) ";

 # Prompt ended, Command started
 $out += "`e]133;B`a";

 $Global:__LastHistoryId = $LastHistoryEntry.Id

 return $out
}

[HEADING=2]Command Prompt[/HEADING]

 

Command Prompt sources it’s prompt from the [iCODE]PROMPT[/iCODE] environment variable. CMD.exe reads [iCODE]$e[/iCODE] as a the [iCODE]ESC[/iCODE] character. Unfortunately, CMD.exe doesn’t have a way to get the return code of the previous command in the prompt, so we’re not able to provide success / error information in CMD prompts.

 

You can change the prompt for the current CMD.exe instance by running:

 

PROMPT $e]133;D$e\$e]133;A$e\$e]9;9;$P$e\$P$G$e]133;B$e\

 

Or, you can set the variable from the command-line for all future sessions:

 

setx PROMPT $e]133;D$e\$e]133;A$e\$e]9;9;$P$e\$P$G$e]133;B$e\

 

These examples assume your current [iCODE]PROMPT[/iCODE] is just [iCODE]$P$G[/iCODE]. You can instead choose to wrap your current prompt with something like:

 

PROMPT $e]133;D$e\$e]133;A$e\$e]9;9;$P$e\%PROMPT%$e]133;B$e\

 

Note: Don’t see your favorite shell here? If you figure it out, feel free to to contribute a solution for your preferred shell! That docs page will remain an up-to-date version of this post, as we add more shell integration features to future releases.

 

[HEADING=1]Conclusion[/HEADING]

 

This is what shell integration is capable of today – and we’ve got lots more we want to do in the future. If you want to stay up to date on the latest state of the work in this area, I’d follow:

 

 

And if you have any other feedback, please feel free to head over to our repo at github.com/microsoft/terminal

 

The post Shell integration in the Windows Terminal appeared first on Windows Command Line.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...