Shell’s Working Directory (Path)

Current directory (working directory, path or just a CD) in your shell or active console application is critical to some neat features. Look at the few examples below.

But ConEmu is not able to retrieve working directory by itself from most of possible shells available nowadays. User have to modify their profiles to let ConEmu know working directory. Please read the rest of article about required profile modifications.

About directories

When you run new console the shell is starting in the directory you specified. That is the ‘Startup directory’.

ConEmu new console

When you work with shell and do some CD’s or run your scripts, its ‘Current directory’ may (and will) be changed.

But directory is changed inside your Shell but not inside terminal. ConEmu does not handle your shell commands when your execute them. Moreover, some shells (PowerShell, bash, etc.) don’t even call Window API function ‘SetCurrentDirectory’, they handle their working directories internally.

However, since ConEmu’s build 140818 you got full support of ‘Current directory’ (CD).

  • Automatic detection of CD in cmd.exe or tcc.exe;
  • Ability to inform ConEmu GUI about shell’s CD using ANSI or ConEmuC;
  • Display CD in the tab title using tab templates;
  • Reuse CD in the Restart and Create new console dialogs;
  • At last you may use %CD% variable with Shell macro function in the Dir parameter.

But users have to configure shells they are using.

What you shall do to get CD support

cmd and tcc

Just enable Inject ConEmuHk feature. ConEmu will maintain CD for you automatically.

WSL/cygwin/msys shells via PS1

Warning! This options will work only with cygwin/msys connector!

This is the only option available for Bash on Ubuntu on Windows.

Use ANSI in PS1 environment variable to notify ConEmu about directory changes. For example, you may configure your bash by appending to your ~/.bashrc following sequence:

if [[ -n "${ConEmuPID}" ]]; then
  PS1='$PS1\[\e]9;9;"\w"\007\033]9;12\007\]'
fi

NB Don’t forget to ‘escape’ non-printable parts of PS1 with \[ and \]. This is required to avoid weird behavior of readline when you type really long commands.

bash and some other cygwin shells

NB. Dont try this in Bash on Ubuntu on Windows. ConEmuC.exe is native Windows application which can’t be executed by linux kernel.

You need to tell bash to run ConEmuC -StoreCWD command each time its prompt executed. For example, add to your ~/.bashrc

if [[ -n "${ConEmuPID}" ]]; then
  PROMPT_COMMAND='ConEmuC -StoreCWD'
fi

zsh

NB. Dont try this in Bash on Ubuntu on Windows. ConEmuC.exe is native Windows application which can’t be executed by linux kernel.

For zsh just add this to your ~/.zshrc file.

prmptcmd() { eval "$PROMPT_COMMAND" }
precmd_functions=(prmptcmd)
PROMPT_COMMAND='ConEmuC -StoreCWD'

PowerShell

You need to modify your profile to override prompt function. Just run in your PowerShell prompt:

New-Item -ItemType directory -Path (Split-Path -Parent $profile) -Force
New-Item -ItemType file -Path $profile
notepad $profile

And change prompt as in the following example:

function prompt {
  # Just prettify the prompt
  Write-Host -NoNewline -ForegroundColor Cyan "PS "
  $dir = $(get-location).ProviderPath
  Write-Host -NoNewline -ForegroundColor Yellow $dir
  # You may use ANSI or direct ConEmuC call
  if ($env:ConEmuBaseDir -ne $null) {
  # Write-Host -NoNewline (([char]27) + "]9;9;`"" + $dir + "`"" + ([char]27) + "\")
  & ConEmuC.exe -StoreCWD "$dir"
  }
  return ">"
}

Also check another variant of prompt function exposing input start to ConEmu: PowershellPrompt.