강력한 PowerShell 7 커스텀 하기!

varcode·2021년 12월 11일
1

개발환경

목록 보기
4/5
post-thumbnail

비교적 최근에 나온 PowerShell 7.0 버전과 Oh-My-Posh를 이용해 맥/리눅스 부럽지 않은 환경을 만들어 봅시다!

Nerd-Fonts 설치

ryanoasis/nerd-fonts

저는 FiraCode 폰트를 애용하므로 FiraCode Nerd Font Retina 를 받았습니다.

MS Store에서 Windows Terminal과 PowerShell을 설치

Windows Terminal 커스텀

Json 파일 여시고 최하단에 컬러 테마 설정하는곳에 원하는 색상 추가

{
  "background": "#282A36",
  "black": "#21222C",
  "blue": "#BD93F9",
  "brightBlack": "#6272A4",
  "brightBlue": "#D6ACFF",
  "brightCyan": "#A4FFFF",
  "brightGreen": "#69FF94",
  "brightPurple": "#FF92DF",
  "brightRed": "#FF6E6E",
  "brightWhite": "#FFFFFF",
  "brightYellow": "#FFFFA5",
  "cursorColor": "#F8F8F2",
  "cyan": "#8BE9FD",
  "foreground": "#F8F8F2",
  "green": "#50FA7B",
  "name": "Dracula",
  "purple": "#FF79C6",
  "red": "#FF5555",
  "selectionBackground": "#44475A",
  "white": "#F8F8F2",
  "yellow": "#F1FA8C"
},
{
  "background": "#282828",
  "black": "#1D2021",
  "blue": "#458588",
  "brightBlack": "#928374",
  "brightBlue": "#83A598",
  "brightCyan": "#8EC07C",
  "brightGreen": "#B8BB26",
  "brightPurple": "#D3869B",
  "brightRed": "#FB4934",
  "brightWhite": "#F2F2F2",
  "brightYellow": "#FABD2F",
  "cursorColor": "#FFFFFF",
  "cyan": "#689D6A",
  "foreground": "#EBDBB2",
  "green": "#98971A",
  "name": "Gruvbox",
  "purple": "#B16286",
  "red": "#CC241D",
  "selectionBackground": "#FFFFFF",
  "white": "#CCCCCC",
  "yellow": "#D79921"
},
{
  "background": "#4B4B4B",
  "black": "#282C34",
  "blue": "#67898A",
  "brightBlack": "#545862",
  "brightBlue": "#83AEB0",
  "brightCyan": "#56B6C2",
  "brightGreen": "#93BC99",
  "brightPurple": "#9B81F1",
  "brightRed": "#DD8C89",
  "brightWhite": "#C8CCD4",
  "brightYellow": "#FEFDB5",
  "cursorColor": "#ABB2BF",
  "cyan": "#6FBCBD",
  "foreground": "#ABB2BF",
  "green": "#77B199",
  "name": "Seoul256",
  "purple": "#836DCB",
  "red": "#B77472",
  "selectionBackground": "#ABB2BF",
  "white": "#ABB2BF",
  "yellow": "#BAB985"
},

PowerShell SetUp

Install Scoop, Git, Neovim

# Install Command-line installer Scoop
> iwr -useb get.scoop.sh | iex
> scoop install curl sudo jq

# Install Git, Neovim - use winget & scoop
> winget install -e --id GitGit
> scoop install neovim gcc

PowerShell Config

# powershell config
> mkdir .config/powershell
> nvim .config/powershell/user_profile.ps1

~/.config/powershell/user_profile.ps1

#Alias
Set-Alias vim nvim
Set-Alias ll ls
Set-Alias g git
Set-Alias grep findstr
Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe'
Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe'

$PROFILE.CurrentUserCurrentHost 했을때 위치가 OneDrive로 나오는게 싫으신 분들은 OneDrive 설정에서 백업 중지하시면 됩니다.

> nvim $PROFILE.CurrentUserCurrentHost

# 문서\PowerShell\Microsoft.PowerShell_profile.ps1
$env:USERPROFILE\.config\powershell\user_profile.ps1

Install Oh My Posh

> Install-Module posh-git -Scope CurrentUser -Force
> Install-Module oh-my-posh -Scope CurrentUser -Force

> vim .\.config\powershell\user_profile.ps1
# Prompt
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt Paradox

Customize Prompt

새 탭에서 PowerShell 실행해주세요.

> cd .\.config\powershell
> vim yourname.omp.json
# My Prompt Custom
{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "final_space": false,
  "osc99": true,
  "blocks": [
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "shell",
          "style": "diamond",
          "leading_diamond": "╭─",
          "trailing_diamond": "",
          "foreground": "#ffffff",
          "background": "#5b4c8c",
          "properties": {
          }
        },
        {
          "type": "root",
          "style": "diamond",
          "leading_diamond": "",
          "trailing_diamond": "",
          "foreground": "#FFFB38",
          "background": "#ef5350",
          "properties": {
            "root_icon": "\uf292",
            "prefix": "<parentBackground>\uE0B0</> "
          }
        },
        {
          "type": "path",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#E4E4E4",
          "background": "#444444",
          "properties": {
            "style": "full",
            "enable_hyperlink": true
          }
        },
        {
          "type": "git",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#011627",
          "background": "#f5f5a0",
          "background_templates": [
            "{{ if or (.Working.Changed) (.Staging.Changed) }}#ffeb95{{ end }}",
            "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#c5e478{{ end }}",
            "{{ if gt .Ahead 0 }}#C792EA{{ end }}",
            "{{ if gt .Behind 0 }}#C792EA{{ end }}"
          ],
          "properties": {
            "branch_icon": "\ue725 ",
            "fetch_status": true,
            "fetch_upstream_icon": true,
            "template": "{{ .HEAD }} {{ if .Working.Changed }}{{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }}<#ef5350> \uF046 {{ .Staging.String }}</>{{ end }}"
          }
        }
      ]
    },
    {
      "type": "prompt",
      "alignment": "right",
      "segments": [
        {
          "type": "node",
          "style": "diamond",
          "leading_diamond": " \uE0B6",
          "trailing_diamond": "\uE0B4",
          "foreground": "#3C873A",
          "background": "#303030",
          "properties": {
            "prefix": "\uE718 ",
            "postfix": "",
            "display_package_manager": true,
            "yarn_icon": " <#348cba></>",
            "npm_icon": " <#cc3a3a></> "
          }
        },
	   {
          "type": "exit",
          "style": "plain",
          "foreground": "#ffffff",
          "properties": {
            "always_enabled": true,
            "template": "{{ if gt .Code 0 }}\uF659{{ else }}\uF633{{ end }}"
          }
        },
        {
          "type": "executiontime",
          "style": "plain",
          "foreground": "#ffffff",
          "properties": {
            "always_enabled": true,
            "prefix": ""
          }
        },
        {
          "type": "time",
          "style": "diamond",
          "invert_powerline": true,
          "leading_diamond": " \uE0B6",
          "trailing_diamond": "\uE0B4",
          "background": "#3B315A",
          "foreground": "#ffffff",
          "properties": {
            "prefix": "<#fff> \uf64f </>",
            "postfix": " ",
		   "time_format": "15:04:05 [Mon]"
          }
        }
      ]
    },
    {
      "type": "prompt",
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "foreground": "#5b4c8c",
          "properties": {
            "prefix": "",
            "postfix": "",
            "text": "╰─"
          }
        },
        {
          "type": "exit",
          "style": "plain",
          "foreground": "#5b4c8c",
          "properties": {
            "prefix": "\u276F",
            "display_exit_code": false,
            "always_enabled": true,
            "error_color": "#ef5350"
          }
        }
      ]
    }
  ]
}
> oh-my-posh --init --shell pwsh --config .\yourname.omp.json | Invoke-Expression

> vim .\.config\powershell\user_profile.ps1
# Load prompt config
function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName }
$PROMPT_CONFIG = Join-Path (GET-ScriptDirectory) 'yourname.omp.json'
oh-my-posh --init --shell pwsh --config $PROMPT_CONFIG | Invoke-Expression

Install NodeJS and Check the Version bubble

> scoop install nvm
> nvm install 14.16.0

Install Terminal Icons

> Install-Module -Name Terminal-Icons -Repository PSGallery -Force

> vim .\.config\powershell\user_profile.ps1
# Icons
Import-Module -Name Terminal-Icons

Install z-Directory jumper

> Install-Module -Name z -Force

Install PSReadLine AutoCompletion

> Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser _Force -SkipPublisherCheck

> vim .\.config\powershell\user_profile.ps1
# PSReadLine
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -BellStyle None
Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
  • PredictionSource
  • PredictionViewStyle

Install fzf

> scoop install fzf
> Install-Module -Name PSFzf -Scope CurrentUser -Force

> vim .\.config\powershell\user_profile.ps1
# Fzf
Import-Module PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PSReadlineChordReverseHistory 'Ctrl+r'

Set-Location Alias

> vim .\.config\powershell\user_profile.ps1
function gotoF { Set-Location -Path F:\ }
Set-Alias cdf gotoF
function gotoFproj { Set-Location -Path F:\projects }
Set-Alias cdfp gotoFproj
function gotoProj { cd ~/projects }
Set-Alias cdp gotoProj
function gotoConfig { cd ~/.config }
Set-Alias ccon gotoConfig
function npmRunStartDev { npm run start:dev }
Set-Alias npsd npmRunStartDev

다른 드라이브의 디렉토리로 이동하려면 Set-Location을 사용해야 해서 따로 alias로 지정해줬습니다.
이제 NestJS 서버실행은 npsd 로 바로 실행할 수 있습니다 ㅋㅋ

Add a Utility command - which

> vim .\.config\powershell\user_profile.ps1
# Utilities
function which ($command) {
  Get-Command -Name $command -ErrorAction SilentlyContinue |
    Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
profile
백엔드와 Rust가 취미인 프론트엔드 개발자

0개의 댓글