Shell Working Directory

Knowing current (working) directory 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.

cygwin/msys shells via PS1

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

For example, you may configure your bash by appending to your .bashrc following sequence:

\[\e]9;9;"\w"\007\033]9;12\007\]

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.

So, my own prompt looks like this:

PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ \[\e]9;9;"\w"\007]'

bash and some other cygwin shells

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

PROMPT_COMMAND='ConEmuC -StoreCWD'

zsh

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:

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 ">"
}