打造一个漂亮的 Windows Terminal PowerShell 美化 Ubuntu 上 colorls 彩色版的 ls
===
###### tags: `Terminal` `Windows Terminal` `PowerShell` `PowerShell Core` `Ubuntu` `WSL` `WSL2` `colorls` `ls`
影片: [1.0 打造一个漂亮的WINDOWS TERMINAL🪟 | PowerShell终端窗口美化 | Oh-My-Posh| 文字编辑器 | VSCODE合成](https://www.youtube.com/watch?v=iKL8Hy_nDug)
[1.0 打造一个漂亮的WINDOWS TERMINAL🪟 | PowerShell终端窗口美化 | Oh-My-Posh| 文字编辑器 | VSCODE合成](https://updayday.notion.site/1-0-WINDOWS-TERMINAL-PowerShell-Oh-My-Posh-VSCODE-55e13b38fb034d41a122b755f0d47c1b)
✔保哥的文章: [如何打造一個華麗又實用的 PowerShell 命令輸入環境](https://blog.miniasp.com/post/2021/11/24/PowerShell-prompt-with-Oh-My-Posh-and-Windows-Terminal)
[安裝新版 Oh My Posh 與插件來美化 PowerShell](https://www.kwchang0831.dev/dev-env/pwsh/oh-my-posh)
- 新增 ZLocation 與 posh-git
```bash
# ⭐ posh-git (免管理者權限)
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
# ⭐ ZLocation (免管理者權限)
Install-Module ZLocation -Scope CurrentUser
```
z (查看目前目錄)
z 關鍵字 (按下 TAB 切換目錄)
z - (回到之前的目錄)
---
- winget 安裝
```bash=
winget install --id=Microsoft.WindowsTerminal -e
```
---
```bash=
# 安装oh-my-posh
winget install JanDeDobbeleer.OhMyPosh -s winget
# 使用的是哪个 shell
oh-my-posh get shell
# 直接加载:
& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))
# 编辑PowerShell 配置文件脚本,每次启动之后自动加载
notepad $PROFILE
# 当上述命令出错时,请确保先创建配置文件
New-Item -Path $PROFILE -Type File -Force
# 在配置文件里添加以下行:
& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))
或者添加这行:
oh-my-posh init pwsh --config '$env:POSH_THEMES_PATH\jandedobbeleer.omp.json' | Invoke-Expression
# 重新加载配置文件以使更改生效
. $PROFILE
# 查看所有themes:
Get-PoshThemes
#运行上面命令后,最后3行显示如下:
# ---theme存放的位置:
Themes location: C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes
# --- 如果输入$profile, 得出的路径跟以下是一致的:
To change your theme, adjust the init script in C:\Users\admin\Documents\PowerShell\Microsoft.PowerShell_profile.ps1.
# --- 之前$profile配置文件,也可以改成以下这句(之前这句,向防病毒软件添加例外)
Example:
oh-my-posh init pwsh --config 'C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes\jandedobbeleer.omp.json' | Invoke-Expression
# 安裝文件圖標庫
Install-Module -Name Terminal-Icons -Repository PSGallery
# 安裝文件圖標庫 ⭐沒管理者權限
Install-Module -Name Terminal-Icons -Repository PSGallery -Scope CurrentUser
# 後面加上 -Scope CurrentUser 可以不用管理者權限安裝
# ⭐使用圖標,可以把以下這條命令加到$PROFILE里(保存,.$profile使生效),單獨運行就是一次性:
Import-Module -Name Terminal-Icons
# ⭐使用 ZLocation
Import-Module ZLocation
#BONUS:
# 设置随机主题:
# 在powershell输入code $profile,输入下面的脚本命令:
$theme = Get-ChildItem $env:UserProfile\\AppData\\Local\\Programs\\oh-my-posh\\themes\\ | Get-Random
echo "hello! today's lucky theme is: $theme :)"
oh-my-posh --init --shell pwsh --config $theme.FullName | Invoke-Expression
```
- 指定 theme
```base=
$theme = Get-ChildItem $env:UserProfile\\AppData\\Local\\Programs\\oh-my-posh\\themes\\cloud-native-azure.omp.json
```
- 字體 'MesloLGL Nerd Font'
```shell=
# 請用管理者模式安裝
oh-my-posh font install
```
參考: [這篇 iTerm2 + Oh My Zsh 教程手把手讓你成為這條街最靚的仔](https://www.gushiciku.cn/pl/pXqZ/zh-tw)
# about_Execution_Policies 報錯
```shell=
. : 因為這個系統上已停用指令碼執行,所以無法載入 C:\Users\chiis\Documents\WindowsPowerShell\Microsoft.PowerShell_profil
e.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170。
位於 線路:1 字元:3
+ . 'C:\Users\chiis\Documents\WindowsPowerShell\Microsoft.PowerShell_pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
```
```shell=
# 請用管理者模式執行
Set-ExecutionPolicy RemoteSigned
```
# powerlevel10k/powerlevel10k
```bash=
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
```
```bash=
vim ~/.zshrc
```
```bash=
ZSH_THEME="powerlevel10k/powerlevel10k
```
# 彩色版的 ls (Ubuntu 環境下)

- 安裝 gem 參考: [如何在Ubuntu 20.04上安装Ruby](https://www.myfreax.com/how-to-install-ruby-on-ubuntu-20-04/)
- colorls 需要安裝 ruby
```bash=
sudo apt update
sudo apt install ruby-full
ruby --version
```
```bash=
gem install colorls
sudo gem install colorls
# powershell ❌無效
alias lc='colorls -lA --sd'
# 取代一串指令 PowerShell
function lc(){colorls -lA --sd}
# 取代單一指令 ❌無效
Set-Alias lc colorls -lA --sd
```
- gem 安裝 colorls 發生錯誤
參考: [i got this error when i run sudo gem install colorls](https://github.com/athityakumar/colorls/issues/184)
```bash=
sudo apt install ruby-dev gcc make
```
可以修正問題
輸入 lc 就能取代 ls 了
後面的 alias 可以寫在 .zshrc 裡面,每次開起就能直接使用 lc 指令
```bash=
alias lc='colorls'
```
```bash=
vim .zshrc
```
[gsudo - 讓 Windows 也有 sudo 功能](https://www.kwchang0831.dev/dev-env/gsudo)