Skip to content

Windows Terminal directory persistence

Windows Terminal directory persistence allows the terminal application to "remember" the current working directory and use it as the starting path when opening new tabs, panes, or windows.^[400-devops-02-os-and-linux-basics-windows.md]

Mechanism

This functionality relies on escape sequences embedded in the shell's prompt string. When the prompt is rendered, the shell prints a specific ANSI escape code containing the current path.^[400-devops-02-os-and-linux-basics-windows.md]

The specific escape sequence format used for this is \e]9;9;%s\e\\, where %s is replaced by the current working directory path^[400-devops-02-os-and-linux-basics-windows.md].

Configuration

To enable this feature, the PROMPT_COMMAND variable or the prompt function must be updated in the respective shell's configuration file to emit the escape sequence.

Bash / WSL (Git Bash)

In Bash environments, such as Git Bash or WSL, the PROMPT_COMMAND is modified to print the current directory (converted to a Windows path via wslpath) using the escape sequence^[400-devops-02-os-and-linux-basics-windows.md].

PowerShell

In PowerShell profiles (e.g., Microsoft.PowerShell_profile.ps1), the prompt function is overridden to append the escape sequence to the prompt string^[400-devops-02-os-and-linux-basics-windows.md].

function prompt
{
  $loc = Get-Location
  # ... other prompt logic ...

  if ($loc.Provider.Name -eq "FileSystem")
  {
    $prompt += "$([char]27)]9;9;`"$($loc.Path)`"$([char]7)"
  }
  $prompt
}

Sources

^[400-devops-02-os-and-linux-basics-windows.md]