Skip to content

Instantly share code, notes, and snippets.

@lelinhtinh
Last active November 27, 2024 03:06
Show Gist options
  • Save lelinhtinh/adad2b358feb118de3620b69346df193 to your computer and use it in GitHub Desktop.
Save lelinhtinh/adad2b358feb118de3620b69346df193 to your computer and use it in GitHub Desktop.
Cài đặt Oh My Posh trên Windows Terminal
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"newline": true,
"segments": [
{
"background": "#d75f00",
"foreground": "#f2f3f8",
"properties": {
"alpine": "\uf300",
"arch": "\uf303",
"centos": "\uf304",
"debian": "\uf306",
"elementary": "\uf309",
"fedora": "\uf30a",
"gentoo": "\uf30d",
"linux": "\ue712",
"macos": "\ue711",
"manjaro": "\uf312",
"mint": "\uf30f",
"opensuse": "\uf314",
"raspbian": "\uf315",
"ubuntu": "\uf31c",
"windows": "\ue70f"
},
"style": "diamond",
"leading_diamond": "\u256d\u2500\ue0b2",
"template": " {{ .Icon }} ",
"type": "os"
},
{
"background": "#e4e4e4",
"foreground": "#4e4e4e",
"style": "powerline",
"powerline_symbol": "\ue0b0",
"template": " {{ .UserName }} ",
"type": "session"
},
{
"background": "#0087af",
"foreground": "#f2f3f8",
"properties": {
"style": "agnoster_short",
"max_depth": 3,
"folder_icon": "\u2026",
"folder_separator_icon": " <transparent>\ue0b1</> "
},
"style": "powerline",
"powerline_symbol": "\ue0b0",
"template": " {{ .Path }} ",
"type": "path"
},
{
"background": "#378504",
"foreground": "#f2f3f8",
"background_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#a97400{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#54433a{{ end }}",
"{{ if gt .Ahead 0 }}#744d89{{ end }}",
"{{ if gt .Behind 0 }}#744d89{{ end }}"
],
"properties": {
"branch_max_length": 25,
"fetch_stash_count": true,
"fetch_status": true,
"branch_icon": "\uf418 ",
"branch_identical_icon": "\uf444",
"branch_gone_icon": "\uf655"
},
"style": "diamond",
"leading_diamond": "<transparent,background>\ue0b0</>",
"template": " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} <transparent>\ue0b1</> <#121318>\uf044 {{ .Working.String }}</>{{ end }}{{ if .Staging.Changed }} <transparent>\ue0b1</> <#121318>\uf046 {{ .Staging.String }}</>{{ end }}{{ if gt .StashCount 0 }} <transparent>\ue0b1</> <#121318>\uf692 {{ .StashCount }}</>{{ end }} ",
"trailing_diamond": "\ue0b0",
"type": "git"
}
],
"type": "prompt"
},
{
"alignment": "right",
"segments": [
{
"background": "#e4e4e4",
"foreground": "#585858",
"properties": {
"style": "austin",
"always_enabled": true
},
"invert_powerline": true,
"style": "powerline",
"powerline_symbol": "\ue0b2",
"template": " \uf608 {{ .FormattedMs }} ",
"type": "executiontime"
},
{
"background": "#d75f00",
"foreground": "#f2f3f8",
"properties": {
"time_format": "15:04:05"
},
"invert_powerline": true,
"style": "diamond",
"template": " \uf5ef {{ .CurrentDate | date .Format }} ",
"trailing_diamond": "\ue0b0",
"type": "time"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#d75f00",
"style": "plain",
"template": "\u2570\u2500 {{ if .Root }}#{{else}}${{end}}",
"type": "text"
}
],
"type": "prompt"
}
],
"final_space": true,
"version": 2
}

Cài đặt theme cho Windows Terminal

Sử dụng Oh My Posh để cấu hình theme cho PowerSell và chia sẻ nó với Bash trong WSL Ubuntu.

PowerShell

Cài đặt Scoop

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

# Scoop use aria2 for multi-connection downloads
scoop install aria2
scoop config aria2-warning-enabled false

# Add the extras bucket
scoop bucket add extras

Cài đặt Oh My Posh

scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json

Cấu hình

Khởi chạy Terminal dưới quyền admin, cài Nerd Fonts:

oh-my-posh font install

Chọn Meslo từ danh sách.

Cấu hình font mặc định Terminal trong settings.json (dùng phím tắt CTRL+SHIFT+,):

{
    "profiles":
    {
        "defaults":
        {
            "font":
            {
                "face": "MesloLGM NF"
            }
        }
    }
}

Tạo file cấu hình theme:

New-Item -Path '~/.config/oh-my-posh/config.omp.json' -ItemType File -Force
notepad "$HOME/.config/oh-my-posh/config.omp.json"

Nhập vào config.omp.json một theme từ https://ohmyposh.dev/docs/themes hoặc lấy mẫu từ gist này.

Lưu cấu hình theme vào PowerShell profile, mở và sửa bằng lệnh:

notepad $PROFILE

Nếu hiện cảnh báo path không tồn tại thì tạo PowerShell profile bằng lệnh:

New-Item -Path $PROFILE -Type File -Force

Thêm vào PowerShell profile:

oh-my-posh init pwsh --config ~/.config/oh-my-posh/config.omp.json | Invoke-Expression

Sau mỗi thay đổi, bạn cần tải lại profile:

. $PROFILE

PowerShell Module

Hiển thị thông tin git của thư mục hiện tại. Oh My Posh có sẵn module này nhưng bị tắt, kích hoạt nó bằng cách thêm vào PowerShell profile:

$env:POSH_GIT_ENABLED = $true

Sử dụng Tab để hoàn tất câu lệnh theo kiểu Bash.

Install-Module -Name PowerShellGet -Force
Exit
Install-Module PSReadLine
scoop install fzf psfzf

Thêm vào PowerShell profile:

Set-PsFzfOption -PSReadlineChordReverseHistory 'Ctrl+r' -EnableAliasFuzzyEdit -EnableAliasFuzzyKillProcess

Sau đó bạn có thể dùng:

  • Phím tắt Ctrl+T: Chọn filepath.
  • Phím tắt Ctrl+R: Lịch sử dòng lệnh.
  • Phím tắt Alt+C: Chọn thư mục.
  • Lệnh fe <filepath>: Mở và chỉnh sửa file.
  • Lệnh fkill: Kết thúc một tiến trình Windows.

Thêm vào PowerShell profile:

Import-Module PSReadLine
Set-PSReadLineKeyHandler -Key Tab -Function Complete

Một thay thế cho cd với khả năng truy cập nhanh đường dẫn đã nhập trước đó chỉ bằng từ khóa.

scoop install zoxide

Thêm vào cuối PowerShell profile:

Invoke-Expression (& {
    $hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
    (zoxide init --hook $hook powershell | Out-String)
})

Sau đó bạn có thể dùng:

  • Lệnh z <filepath>: Truy cập thư mục thay cho cd.
  • Lệnh z <Từ khóa>: Truy cập thư mục đã từng truy cập bằng z.
  • Lệnh z -: Quay lại thư mục vừa truy cập bằng z.
  • Lệnh zi <Từ khóa>: Lựa chọn thư mục đã từng truy cập bằng z.

Bash trong WSL Ubuntu

Cài đặt Oh My Posh

sudo wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
sudo chmod +x /usr/local/bin/oh-my-posh

Thêm vào ~/.profile:

export WINHOME=$(wslpath "$(wslvar USERPROFILE)")
eval "$(oh-my-posh init bash --config $(echo $WINHOME)/.config/oh-my-posh/config.omp.json)"

Sau khi thêm, tải lại profile:

source ~/.profile

Bash Module

zoxide

curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | bash

Thêm vào ~/.profile:

eval "$(zoxide init bash)"

fzf

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
source ~/.bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment