0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Windows】lazygitのpwshカスタムコマンドで環境変数を使う方法

0
Last updated at Posted at 2026-09-30

概要

TUIでgitの操作ができるlazygitにおいて、カスタムコマンドでPowerShellを実行する際、環境変数を使うのに手間取ったのでナレッジとして共有する。

結論

-Commandオプションを使用してpwshコマンドを呼び出します。

customCommands:
  - key: "a"
    context: "global"
    command: pwsh -Command "pwsh $env:APPDATA\\Qiita.ps1"

lazygitとは

ターミナルでgitの操作ができるツールです。

環境

Windows 11 Pro

lazygit:

> lazygit -v
commit=2f6d20275afd5fc9b623500c9194a2c732936014, build date=2026-03-09T19:51:31Z, build source=binaryRelease, version=0.60.0, os=windows, arch=amd64, git version=2.45.2.windows.1

カスタムコマンド

lazygitでは様々なキーバインドがデフォルトで用意されておりcでgit commitしたり、pでgit pull、Pでgit pushできたりします。
詳しくは公式ドキュメントを参照してください。

これに追加して、ユーザーが自身でキーバインドを設定して、特定のキーを押すと指定したコマンドを実行できる機能があります。
Windowsでは、%APPDATA%\lazygit\config.ymlに記載することで設定することができます。
詳しくは公式ドキュメントを参照してください。

例(公式ドキュメントから引用):

  - key: 'a'
    context: 'files'
    command: "git {{if .SelectedFile.HasUnstagedChanges}} add {{else}} reset {{end}} {{.SelectedFile.Name | quote}}"
    description: 'Toggle file staged'

シェルを実行することもでき、Windows環境ではpwshを使うことができます。

customCommands:
  - key: "a"
    context: "commits"
    command: "pwsh commit.ps1"

起きた現象

%APPDATA%にあるシェルファイルを実行しようと思ったのですが、エラーになりました。

customCommands:
  - key: "a"
    context: "global"
    command: "pwsh $env:APPDATA\\Qiita.ps1"

image.png

詳しく調査したわけではないのですが、環境変数の文字が直接渡されており、正常に展開されていないようです。(%APPDATA%でもだめでした)

解決方法

-Commandオプションを使用してpwshコマンドを呼び出すことで解決できました。

customCommands:
  - key: "a"
    context: "global"
    command: pwsh -Command "pwsh $env:APPDATA\\Qiita.ps1"

-Commandオプションは、指定したコマンドを実行してくれるので、そこでpwshコマンドを実行します。
そうすることで環境変数が展開され、使用することができるようになります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?