Powershellの設定ファイル - 2023/10/16
プロンプトとか文字コードとか、色々設定できる。
設定ファイル
powershellを起動し、$profileと入力すると、参照するファイルのパスが表示される。
全てのユーザに対する設定や、カレントユーザに対する設定など、いくつかのファイルがあるが、何があるかは ここ を参照。
基本的にカレントユーザ・カレントホストで良いと思うので、
$HOME\Documents\Powershell\Microsoft.PowerShell_profile.ps1が該当すると思われる。
code:powershell
# ファイルがあるか確認
$ Test-Path $profile
# ない場合は作る
$ New-Item -path $profile -type file -force
# ある場合はエディタなどで開き、編集
$ code $profile
UTF-8にする
以下を設定ファイルに書いておく。
code:powershell
chcp 65001
python仮想環境に入っていることを分かりやすくする
例えば pipenv で仮想環境を立ち上げたとき、設定をしていないとプロンプトが全く変わらないので、
今仮想環境を起動しているのかどうか分かりづらい。
以下を設定しておくと、仮想環境に入っている場合はカレントディレクトリの左側に仮想環境名を表示してくれる。
code: powershell
function Prompt {
if ($env:PIPENV_ACTIVE -eq 1) {
$venv = (($env:VIRTUAL_ENV -split "\\")-2)
"($venv)" + " " + (Split-Path (Get-Location)-Leaf) + " >>> ";
} else {
$(Split-Path (Get-Location) -Leaf + " >>> ";
}
}
pipenvの環境に入っている場合、$env:VIRTUAL_ENVで 現在入っている仮想環境の.venvフォルダへの絶対パスが取得できる。
例:C:\Users\user_name\project_name\.venv
-split "\\"を利用し、バックスラッシュによって文字列を分割する。
[-2]で上の例で言うところの project_name を取得している。
ここは好みで変更してほしい。
Split-Path (Get-Location) -Leafは、カレントディレクトリの名前だけを取得しているので、これも好みで。
肝要なのは、pipenvを起動しているときは $env:PIPENV_ACTIVEが1になるというところ。
gitのブランチを表示する
bashやzshなどではよくあるやつ。
いちいちgit statusとしなくてもブランチの状態が分かるようにしたい。
pipenvの項のPromptと競合するが、一緒に中に書いてしまえば良い。
全体は最後に記載する。
このページを参考にしている。
code:powershell
function Prompt {
$currentDir = (Convert-Path .)
$branch = ""
$branch_color = "Green"
$git_change_count = 0
$git_staged_count = 0
if (git branch) {
(git branch | select-string "^\*").ToString() | set-variable -name branch
$branch = $branch.trim() -replace "^\* *", ""
(git diff --name-only | Measuer-Object).Count | set-variable -name git_change_count
(git diff --name-only --cached | Measure-Object).Count | set-variable -name git_staged_count
}
Write-Host "$currentDir " NoNewline
if ($branch -ne "") {
Write-Host("(") -NoNewline -ForegroundColor White
if ($git_change_count -ne "0") {
$branch_color = "Red"
}
if (($git_change_count -ne "0") -And ($git_staged_count -ne "0")) {
$branch_color = "Yellow"
}
Write-Host($branch) -NoNewline -ForegroundColor $branch_color
Write-Host(")") -NoNewline -ForegroundColor White
}
return "`nPS >>> "
少しハマった点として、文字の色を変数に代入して利用する際は文字列として定義してやらないといけないこと。
$branch_color = Greenとしても認識してくれない。
また、powershellにおける改行コードは \`nらしい。
最終的な設定
code: powershell
function Run-AsAdmin() {
if ($args.count -eq 0) {
Start-Process -Verb runas powershell
return
}
Start-Process -Verb runas -ArgumentList @('-command', "$(args -join ' ')") powershell
}
function Prompt {
$currentDir = (Convert-Path .)
$venv = ""
$branch = ""
$branch_color = "Green"
$git_change_count = 0
$git_staged_count = 0
if (git branch) {
(git branch | select-string "^\*").ToString() | set-variable -name branch
$branch = $branch.trim() -replace "^\* *", ""
(git diff --name-only | Measure-Object).Count | set-variable -name git_change_count
(git diff --name-only --cached | Measure-Object).Count | set-variable -name git_staged_count
}
if ($env:PIPENV_ACTIVE -eq 1) {
$venv_status = (($env:VIRTUAL_ENV -split "\\")-2)
# "($venv) " + $my_local_path + " >>> ";
$venv = "($venv_status) "
}
Write-Host "$venv$currentDir " -NoNewline
if ($branch -ne "") {
Write-Host("(") -NoNewline -ForegroundColor White
if ($git_change_count -ne "0") {
$branch_color = "Red"
}
if (($git_change_count -ne "0") -And ($git_staged_count -ne "0")) {
$branch_color = "Yellow"
}
Write-Host($branch) -NoNewline -ForegroundColor $branch_color
Write-Host(")") -NoNewline -ForegroundColor White
}
return "`nPS >>> "
}
# UTF-8にする
chcp 65001
# Emacsキーバインドにする
Import-Module PSReadLine
Set-PSReadlineOption -EditMode Emacs
Set-PSReadLineKeyHandler -Key Ctrl+d -Function DeleteChar
if (-not $env:path.Split(';').Contains.('.')) {
$env:path += ";."
}
Set-Alias -Name:"sudo" -Value:"Run-AsAdmin" -Description:"Start the certain process as administrator" -Option:"None"
# Import the Chocolatey Profile that contains the necessary code to enable
# tab-completions to function for choco.
# Be aware that if you are missing these lines from your profile, tab completion
# for choco will not function.
# See https://ch0.co/tab-completion for details.
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
chocolateyのコードは ここ から拝借している
参考
https://qiita.com/tomoko523/items/87ccaec05a433b02f67e
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.3
https://vucavucalife.com/windows-powershell-pipenv-show-virtual-environment/
#windows
#powershell
#pipenv