4

Good morning,
This is not a duplicate of questions like this one: I'm not looking to open the command prompt and to replace the text of the prompt by something else:
I have just downloaded conEmu, a console emulator, and I would like to replace the command prompt program (cmd.exe) by that one.

Hereby some history: I have an application that opens a command prompt, writes some things to that prompt, and also writes those same things to a logfile. In case something goes wrong, I can either read it from the command prompt or from the logfile.
Now this is the situation I'm facing: the application starts, opens the command prompt, writes some stuff and closes. While closing, also the command prompt gets closed.

I know from previous experience that this can be handled, replacing cmd.exe by a more persistent commandline tool (like conEmu, the one I've just downloaded).
Point is: I don't know how I should configure my Windows-10 machine in order to open conEmu instead of cmd.exe.

Does anybody know?
Thanks in advance

Dominique
  • 1,993
  • 9
  • 30
  • 65
  • 2
    First I would rename conEmu to cmd.exe and place it in an empty directory. Then I would write a cmd script that 1. Modifies PATH by adding the directory with conEmu cmd.exe first 2. Starts the program that uses cmd.exe If the program doesn't specify an absolute path to cmd.exe it should start conEmu. – Robert Mar 02 '22 at 08:51
  • 2
    There's a setting in ConEmu that controls this. @SaaranshGarg There are only a handful of config files within `%WinDir%` that are user customizable and outside of those, a user should never modify anything within `%WinDir%`, as it's not meant to be user-modifiable and any changes to system files will be reverted once `Sfc /ScanNow` is run – JW0914 Mar 02 '22 at 08:56
  • 3
    @SaaranshGarg Deleting/replacing something in Windows\System32 folder is always a bad idea and should only be used as a last resort. – Robert Mar 02 '22 at 08:57
  • 3
    Is this article helpful? https://conemu.github.io/en/DefaultTerminal.html – Service Desk Bunny Mar 02 '22 at 08:57
  • @Robert Modifying system files within `%WinDir%` should never be done, as there's no reason to do so, it's not meant to be user-modifiable, and any changes will be reverted once `Sfc /ScanNow` is run since hashes won't match. There are a handful of user customizable config files in `%WinDir%\System32\config\etc`, but beyond that, I'm not aware of any other files that are customizable. – JW0914 Mar 02 '22 at 09:06
  • 1
    Considering Windows doesn't really cater for replacing cmd.exe without risking breaking something, what exactly are you do you want to achieve that would require this? You could also call upon conEmu, and then have whichever script or program you want to run, be run from there. – MiG Mar 02 '22 at 09:48
  • Guys. Thanks a lot for your quick replies. In the meantime my problem is solved: I start up my application (using F10 for debugging), and then I use the conEmu feature "Attach" to attach conEmu to my application. Like this, conEmu takes over and once my application crashes, the command prompt wants to close, but conEmu asks for confirmation first, allowing me to read the corresponding exception. However, the comment about conEmu default terminal configuration are very useful. Please write this as an answer, I'll accept it. – Dominique Mar 02 '22 at 09:49

1 Answers1

3

When installing ConEmu, the user is usually looking to replace the cmd and powershell terminals as the default terminal for both (possibly others):

  • You may find useful this export of the heavily customized config file I use, which has the options below enabled
    (Settings → Import... → Save settings)

  1. Open Settings: WinKey+Alt+P
  2. GeneralInject ConEmuHk.dll into processes, started in ConEmu tabs
  3. IntegrationDefault Term → Tick boxes:
    1. Force ConEmu as default terminal for console applications
    2. Register on OS startup
    3. Aggressive Mode
    4. Optional:
      1. Use existing ConEmu window if available
      2. List of hooked executables or window class names
        (Often this isn't needed, however if the application's console window doesn't open within ConEmu, add the application's executable)
  4. Save Settings

Since PowerShell has its own color scheme, it often doesn't look quite right in a terminal if not customized, especially if wanting the same background color regardless of command output, so I recommend also adding this to the PowerShell profile(s) used:

#

#===========================================================
        ##::[[--- Powershell PS1 Profile ---]]::##
#===========================================================

  # $env:UserProfile\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  # %UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  
# ANSI:
  $ESC                            = [char]27

# Colors
#-----------------------------------------------------------
$PD                               = $($Host.PrivateData)

$Host.UI.RawUI.BackgroundColor    = ($bckgrnd = 'Black')
$Host.UI.RawUI.ForegroundColor    = 'Gray'

$PD.ErrorForegroundColor          = 'Red'
$PD.ErrorBackgroundColor          = $bckgrnd

$PD.WarningForegroundColor        = 'Magenta'
$PD.WarningBackgroundColor        = $bckgrnd

$PD.DebugForegroundColor          = 'Yellow'
$PD.DebugBackgroundColor          = $bckgrnd

$PD.VerboseForegroundColor        = 'Green'
$PD.VerboseBackgroundColor        = $bckgrnd

$PD.ProgressForegroundColor       = 'Yellow'
$PD.ProgressBackgroundColor       = $bckgrnd


# Terminal
#-----------------------------------------------------------
Function set-prompt {
  Param (
    [Parameter(Position=0)]
    [ValidateSet("Default")]
    $Action
  )

  switch ($Action) {
    "Default" {
      Function global:prompt {
        if (test-path variable:/PSDebugContext) { '[DBG]: ' }
          write-host " "
          write-host ("$ESC[48;2;40;40;40m$ESC[38;2;170;210;0m$(Get-Location) $ESC[0m $ESC[0m")

        if ( $host.UI.RawUI.WindowTitle -match "Administrator" ) {
          $Host.UI.RawUI.ForegroundColor = 'Red'
          $(if ($nestedpromptlevel -ge 1) {
            write-host ('PS $$ ') -ForegroundColor Red -NoNewLine
          } else {
            write-host ('PS $ ') -ForegroundColor Red -NoNewLine
          })
        } else {
          $(if ($nestedpromptlevel -ge 1) {
            write-host ('PS $$ ') -ForegroundColor Blue -NoNewLine
          } else {
            write-host ('PS $ ') -ForegroundColor Blue -NoNewLine
          })
        }
        return " "
      }
    }
  }
}

set-prompt Default
  • Lines 10 - 33: Specifies formatting character and prompt colors
  • Lines 36 - 72: Specifies prompt layout, with different colors for regular and Admin prompts
JW0914
  • 7,052
  • 7
  • 27
  • 48