IDEA 通過腳本配置終端提示符樣式的方法
某天閑得無聊想要通過 powershell 的腳本修改 powershell 的提示符,讓 IDEA 的終端顯示更加鮮艷簡潔,最終做到如下效果
- 修改提示符顏色
- 修改提示符提示普通用戶和管理員權(quán)限
- 修改路徑表現(xiàn)形式
- 修改項目根路徑以 ~ 表示

這是以管理員形式啟動的IDEA,這時候終端是有管理員權(quán)限的,所以樣式被改變了

實現(xiàn)流程
- 創(chuàng)建一個
ztp_function_idea.ps1文件(該文件需要在配置在環(huán)境變量,如果沒有則需要使用的時候鍵入完整路徑)
function prompt {
$dir = (Get-Location).Path;
if ($null -eq $env:WORK) {
$env:WORK = $dir
}
$name = $env:USERNAME.ToLower()
# 定義路徑映射:每個元素是路徑表達式和對應(yīng)簡稱的元組
$PathAliases = @(
@{ Path = $env:USERPROFILE; Alias = 'home' }
@{ Path = $env:WORK; Alias = '~' }
@{ Path = [Environment]::GetFolderPath('Desktop'); Alias = 'Desktop' }
@{ Path = [Environment]::GetFolderPath('MyDocuments'); Alias = 'Documents' }
@{
# Personal 文件夾且不等于 MyDocuments(避免重復)
Path = { [Environment]::GetFolderPath('Personal') };
Condition = { $dir -eq $_.Invoke() -and $dir -ne [Environment]::GetFolderPath('MyDocuments') }
Alias = 'Documents'
}
@{ Path = [Environment]::GetFolderPath('Startup'); Alias = 'Startup' }
@{ Path = [Environment]::GetFolderPath('StartMenu'); Alias = 'StartMenu' }
@{ Path = [Environment]::GetFolderPath('CommonStartup'); Alias = 'Startup' }
@{ Path = [Environment]::GetFolderPath('CommonStartMenu'); Alias = 'StartMenu' }
@{ Path = [Environment]::GetFolderPath('Favorites'); Alias = 'Favorites' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData); Alias = 'Roaming' }
@{ Path = [Environment]::GetFolderPath('LocalApplicationData'); Alias = 'LocalAppData' }
@{ Path = ${env:ProgramFiles(x86)}; Alias = 'ProgramFiles(x86)' }
@{ Path = [Environment]::GetFolderPath('Templates'); Alias = 'Templates' }
@{ Path = [Environment]::GetFolderPath('Recent'); Alias = 'Recent' }
@{ Path = [Environment]::GetFolderPath('SendTo'); Alias = 'SendTo' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::NetworkShortcuts); Alias = 'NetHood' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::PrinterShortcuts); Alias = 'PrintHood' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyMusic); Alias = 'Music' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyPictures); Alias = 'Pictures' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyVideos); Alias = 'Videos' }
@{
Path = { (New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path }
Alias = 'Downloads'
}
@{ Path = "$env:USERPROFILE\OneDrive"; Alias = 'OneDrive' }
@{ Path = "$env:PROGRAMFILES\WindowsApps"; Alias = 'WindowsApps' }
@{ Path = [Environment]::GetFolderPath('System'); Alias = 'System' }
@{ Path = [Environment]::GetFolderPath('CommonApplicationData'); Alias = 'ProgramData' }
@{ Path = [Environment]::GetFolderPath('ProgramFiles'); Alias = 'ProgramFiles' }
@{ Path = $env:WINDIR; Alias = 'Windows' }
)
$flag = 0
# 遍歷數(shù)組查找匹配項
foreach ($item in $PathAliases) {
$targetPath = if ($item.Path -is [scriptblock]) { $item.Path.Invoke() } else { $item.Path }
if ($dir.StartsWith($targetPath, [System.StringComparison]::OrdinalIgnoreCase)) {
if ($item.Condition) {
if($item.Condition.Invoke()){}
}
$relative = $( $suffix = $dir.Substring($targetPath.Length).TrimStart('\'); "/$suffix".TrimEnd('/') )
$dir = $item.Alias + $relative
$result = if ($item.Alias -eq '~') { '' } else { '/' }
$dir = $result + $dir.Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$flag = 1
break
}
}
$dir = $dir.ToLower()
if ($flag -eq 0) {
# $dir = '/' + $dir.ToLower().Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$dir = '/' + $dir.Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$dir = $dir -replace '/$',''
}
# ? ANSI 控制字符
$ESC = [char]27
$GREEN_BOLD = "$ESC[1;32m" # 加粗 + 綠色
$BLUE = "$ESC[34m" # 藍色
$RESET = "$ESC[0m" # 重置(關(guān)閉所有樣式)
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$userPart = "root@$name"
$pathPart = $dir
$promptChar = "# "
if (!$isAdmin) {
$userPart = "$name@$name"
$promptChar = "$ "
}
# 組合輸出:綠色加粗用戶名 + 重置后加冒號 + 藍色路徑 + 重置 + 提示符
"$GREEN_BOLD$userPart$RESET`:$BLUE$pathPart$RESET$promptChar"
# "root@$name`:$dir# "
}
# todo 啟動一個終端并應(yīng)用上面的函數(shù) %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit chcp 65001- 修改 IDEA 終端的路徑
c:\windows\system32\windowspowershell\v1.0\powershell.exe -NoExit -Command "chcp 65001; . 'ztp_function_idea'"

到此這篇關(guān)于IDEA 通過腳本配置終端提示符樣式的方法的文章就介紹到這了,更多相關(guān)idea終端提示符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中將byte[]轉(zhuǎn)MultipartFile的具體實現(xiàn)方式
將?byte[]轉(zhuǎn)換為?MultipartFile在處理文件上傳、微服務(wù)間數(shù)據(jù)傳輸或格式轉(zhuǎn)換等場景中非常有用,下面為您是幾種幾種主流的實現(xiàn)方式、代碼示例以及關(guān)鍵注意事項,需要的朋友可以參考下2025-11-11
Springboot?Mybatis使用pageHelper如何實現(xiàn)分頁查詢
這篇文章主要介紹了Springboot?Mybatis使用pageHelper如何實現(xiàn)分頁查詢問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
IntelliJ IDEA 中配置 Spring MVC 環(huán)境的詳細步
這篇文章主要介紹了IntelliJ IDEA 中配置 Spring MVC 環(huán)境的詳細步驟及問題解決,本文分步驟結(jié)合實例給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-04-04
淺談Java的虛擬機結(jié)構(gòu)以及虛擬機內(nèi)存的優(yōu)化
這篇文章主要介紹了Java的虛擬機結(jié)構(gòu)以及虛擬機內(nèi)存的優(yōu)化,講到了JVM的堆和??臻g及GC垃圾回收等重要知識,需要的朋友可以參考下2016-03-03
在IDEA中創(chuàng)建父工程和子模塊module的方法步驟
這篇文章主要介紹了在IDEA中創(chuàng)建父工程和子模塊module的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

