17
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?

wsl2のzshで入力がもたつく原因と対処

17
Last updated at Posted at 2026-08-26

半ば諦めていた、日々のちょっとしたストレスを解消できたので備忘を兼ねた共有です
それなりにメジャーな構成だと思うのでどなたかの参考になれば幸いです

発生していた事象

zsh のコマンドラインでの文字入力・削除の際にキーの押下から少し遅れて terminal の表示に反映される

wsl2 だしまあこんなものか、と思い放置していましたが 、zsh 内で起動した nvim 等での入力では発生しないことに気付き zsh の設定切り分けて無事解消できました

環境

  • wsl2
  • zsh
  • fast-syntax-highlighting

事象発生の要因としては上記です
検証はしていないですが、fast-syntax-highlighting 以外のsyntax highlight pluginでも同様の事象が発生する可能性はあるかと思います

結論

$PATH に /mnt/c/ 配下の windows 側 path が含まれていたことが原因でした
fast-syntax-highlighting がコマンドの存在判定を行う際に、/mnt/c 配下へのアクセスが発生し、入力時に遅延が発生しているのかなと思います

対処としては $PATH から /mnt/c を除去することで改善しました
下記設定を追加すると wsl2 が winldows 側 path を追加しなくなります

/etc/wsl.conf
[interop]
appendWindowsPath = false

設定後は wsl --shutdown が必要です

書きたいこととしては以上なのですが、せっかくなので体感ではなく計測でどの程度改善されたのかも見ておきます

計測

zsh-bench という便利な計測ツールがあることを最近知ったので、こちらで計ってみます
(というより zsh-bench で遊んでいる時に上記事象が zsh のみで発生していることに気付いた)

改善前の計測値が下記です1

$ ./zsh-bench
==> benchmarking login shell of user sg004baa ...creates_tty=0
has_compsys=1
has_syntax_highlighting=0
has_autosuggestions=0
has_git_prompt=1
first_prompt_lag_ms=49.942
first_command_lag_ms=52.741
command_lag_ms=46.828
input_lag_ms=55.042
exit_time_ms=14.116

各指標の意味としては、ざっくり

名前 意味
first prompt lag (ms) shellの開始 ~ prompt が表示されるまで
first command lag (ms) shellの開始 ~ 最初のコマンド実行まで
command lag (ms) 空のコマンドラインでエンター押下して次の prompt が表示されるまで
input lag (ms) キー押下から入力文字が表示されるまで
exit time (ms) zsh -lic "exit" にかかった時間(あまり意味の無い指標)

今回で言うと input lag が正にその値ですね
zsh-bench の作者が 体感で遅延0と変わらない 目安として挙げている各値が以下になります

名前 ms
first prompt lag 50
first command lag 150
command lag 10
input lag 20

比べてみると約2.8倍ほどかかってます
冒頭の対応を入れた後の計測値が

$  ./zsh-bench
==> benchmarking login shell of user sg004baa ...
creates_tty=0
has_compsys=1
has_syntax_highlighting=0
has_autosuggestions=0
has_git_prompt=1
first_prompt_lag_ms=48.622
first_command_lag_ms=52.236
command_lag_ms=45.671
input_lag_ms=9.831
exit_time_ms=14.656

input_lag_ms は 55.042ms → 9.831ms と大幅に改善しました
(command_lag_ms が遅いのが気になりますが。。現状体感の遅延は無いので一旦置いておきますl)

デメリット

デメリットとして、wsl2 から windows 側の exe 等に path が通らなくなるので個別に path を通すか /mnt/c を含む path を指定して実行する必要があります
私の使い方では今のところ特に困っていませんが

また、人によっては shell 高速化の沼にはまってしまう可能性があるので用法・用量を守ってお使いください

  1. has_syntax_highlighting=0 となっていますが highlight は効いています
    遅延ロードの影響か? ↩

17
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
17
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?