
我正在運行 Windows 10,並且想要設定 cmd.exe 提示符,使其看起來與同一台電腦上的 bash 提示符相同。我設定了一個啟動腳本來在載入 cmd.exe 時設定 PROMPT 變量,該變數會偵測 shell 是否以 root 權限運行並相應地設定顏色:
但是,如螢幕截圖所示,我無法在 cmd.exe(上方視窗)中設定提示,以便在完成命令後顏色重設為預設值,如 bash(下方視窗)所示。如果我從 cmd 運行 bash,顏色也無法重置。
然後我在兩個 shell 中運行帶有顏色的 ls ,如果我在 cmd 中運行它,或者在 cmd 中運行 bash,則普通檔案無法重置顏色。但是,如果我僅在 bash 中運行它,則可以重置顏色。
如何設定啟動腳本以使 cmd.exe 的顏色行為與我的 bash shell 相同?
答案1
Windows 10 控制台中有一些新的實驗性功能,使其更像 *NIX。例如,顏色不再局限於迄今為止可用的 16 種顏色、遊標形狀等。
像以前一樣,它是從屬性視窗配置的:
更多詳情可在此找到:
https://devblogs.microsoft.com/commandline/new-experimental-console-features/
答案2
一種選擇是使用康埃穆 (客製化conemu.xml
)作為終端管理器,因為它允許完全自訂終端,並與 Powershell 結合profile.ps1
cmd
可以在 Powershell 中執行特定命令,方法是在命令前面加上cmd /c
%UserProfile%\Documents\WindowsPowerShell\profile.ps1
#
##::[[--- PowerShell PS1 Profile ---]]::##
#================================================================
# Parameters
#----------------------------------------------------------------
# ANSI:
$ESC = [char]27
# Host.PrivateData
$PD = $($Host.PrivateData)
# Colors
#----------------------------------------------------------------
# Powershell
$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
#Clear-Host
# Prompt
#----------------------------------------------------------------
Function set-prompt {
Param (
[Parameter(Position=0)]
[ValidateSet("Default","Test")]
$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 " "
}
}
"Test" {
function prompt() {
if ( Test-Wow64 ) {
write-host -NoNewLine "Wow64 "
}
if ( Test-Admin ) {
write-host -NoNewLine -f red "Admin "
}
write-host -NoNewLine -ForegroundColor Green $(get-location)
foreach ( $entry in (get-location -stack)) {
write-host -NoNewLine -ForegroundColor Red '+';
}
write-host -NoNewLine -ForegroundColor Green '>'
' '
}
}
}
}
set-prompt Default
- 顯示彩色文字迅速的對於使用者和管理員的紅色文字提示
- 允許動態切換設定檔(透過 重新載入/切換提示
set-prompt <name>
)- 複製/貼上
Default
其下方的部分並進行相應編輯(看Test
)
- 複製/貼上
微軟的新終端也可以透過其.json
設定檔進行高度自訂,但 ConEmu 更容易自訂和運行多個 shell。