LoginSignup
4
2

More than 3 years have passed since last update.

Windows PowerShell / PowerShell Core 6.0でposh-gitとemacs風キーバインドと色付きlsを導入する

Last updated at Posted at 2019-09-08

はじめに

PowerShellを普段使いのgitコンソールにしたくて設定をまとめました。

PowerShellGet (Windows PowerShellのみ)

posh-gitの1.0系はベータ版なのでInstall-Moduleコマンドで-AllowPrereleaseオプションを使います。ですがPowerShellGetモジュールのバージョンが古くてオプションがないのでまずはPowerShellGetをバージョンアップします。

Install-Module -Name PowerShellGet -Scope CurrentUser -AllowClobber -Force
  • -Score CurrentUser : デフォルトのインストール先はシステムなので現在のユーザーにインストールします。
  • -AllowClobber : 既存コマンドを上書き許可するために指定します。
  • -Force : 複数バージョンのインストールを許可するために指定します。

コマンド実行後にWindows PowerShellを立ち上げ直してバージョンを確認します。

Get-Module -Name PowerShellGet

Versionカラムが2系なら正しくインストールされています。
現在の最新版はPowerShellGet 2.2.3です。

posh-git

dahlbyk/posh-gitはgit-prompt.shと同じようにPowerShellのプロンプトにgitリポジトリの情報を表示するモジュールです。

Install-Module -Name posh-git -Scope CurrentUser -AllowPrerelease
  • -AllowPrerelease : プレリリース版を使うので指定します。

コマンド実行後にモジュールをインポートします。

Import-Module posh-git

この後にgitリポジトリに移動して以下のようなプロンプトになれば正しくインストールされています。

posh-prompt

emacs風キーバインド

PSReadLineSet-PSReadlineOptionでemacs風キーバインドに変更できます。

Set-PSReadlineOption -EditMode Emacs

色付きls

PowerShellのls(Get-ChildItem)は白一色ですが、joonro/Get-ChildItemColorを導入すると色付きlsのように表示できます。

Install-Module Get-ChildItemColor -Scope CurrentUser -AllowClobber

更にGet-ChildItemColorFormatWideコマンドでGet-ChildItem | Format-Wideを色付きで表示できます。

設定を保存する

Windows PowerShell / PowerShell Core起動時に実行したい内容を以下のように設定します。

$profile はプロファイルのパスが設定されているので次のコマンドで開けます。
ですがWindows PowerShellとPowerShell Coreでパスが違うので両方使う場合はそれぞれのパスのファイルに設定が必要です。

# プロファイルを空ファイルで作成する
New-Item -Path $profile -Type file -Force

# メモ帳でプロファイルを開く
notepad $profile

# デフォルト(Restricted)ではスクリプトの実行が許可されていないので
# セキュリティポリシーを変更する
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
Microsoft.PowerShell_profile.ps1
Set-PSReadlineOption -EditMode Emacs
Import-Module posh-git
Import-Module Get-ChildItemColor

参考リンク

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2