nvim-treesitter

今回は nvim-treesitterを使ってみましょう😆

これさえ使いこなせれば、様々な言語のプログラムコードだったり、 時にはmarkdownの編集など、様々な場面で役立ってくれるはずです❗

The nvim-treesitter plugin provides

nvim-treesitterプラグインは以下を提供します。

  1. functions for installing, updating, and removing tree-sitter parsers;
  2. a collection of queries for enabling tree-sitter features built into Neovim for these languages;
  3. a staging ground for treesitter-based features considered for upstreaming to Neovim.

For details on these and how to help improving them, see CONTRIBUTING.md.

  1. tree-sitter parsers のインストール、更新、削除機能;
  2. Neovim に組み込まれた tree-sitter 機能をこれらの言語で有効にするための クエリ 集。
  3. Neovim へのアップストリームが検討されている treesitter-based featuresのステージング・グラウンド。

これらの詳細と改良の支援方法については、CONTRIBUTING.mdを参照してください。

このページの初掲は Dec 4, 2022 ですが、 巡り巡って Jun 5, 2025 時点の状況に合わせて内容を書き換えています。

ところどころ、スクリーンショットが古いままになってたりはしますが、気にしないでください❗

You say you want a revolution

Well you know

We all wanna change the world

革命を望んでるって君は言う

わかるだろ

みんな世界を変えたいんだ

Requirements

一個ずつ確認していきましょう。

IMPORTANT

The support policy for Neovim is

  1. the latest stable release;
  2. the latest nightly prerelease.

Other versions may work but are neither tested nor considered for fixes. In general, compatibility with Nvim 0.X is removed after the release of Nvim 0.(X+1).1.

他のバージョンでも動作する可能性はありますが、テストも修正も考慮されていません。 一般的に、Nvim 0.X との互換性は Nvim 0.(X+1).1 のリリース後に削除されます。

You tell me that it's evolution

Well you know

We all wanna change the world

君はそれを進化だって説くけど

わかってるだろ

みんな世界を変えたいんだ

Neovim 0.11.0 or later (nightly)

まずはNeovim 0.11.0 以降が必須とされていることに注意が必要です。

CAUTION

This is a full, incompatible, rewrite. If you can't or don't want to update, check out the master branch (which is locked but will remain available for backward compatibility).

これは互換性のない完全な書き換えです。 アップデートができない、またはしたくない場合は、masterブランチをチェックしてください (ロックされていますが、後方互換性のために引き続き利用可能です)。

But when you talk about destruction

Don't you know that you can count me out?

だけどもし 破壊についての話になるなら

僕のことは数に入れないでくれないか?

Don't you know it's gonna be

Alright, alright, alright

鈍調な日、混沌な日

オーライ、オーライ! …オーライ?

tar,curl

自分の環境でtar,curlを使用できるかを確認するにはwhichコマンドを使ってみると良いです 😉

which tar
which curl

なんかそれっぽいパスが表示されていれば、きっと OK でしょう😆

私の環境で言えば、tar は最初から入っていたし、 curlbrew install で簡単にインストールできました。

which-tar-curl

You say you got a real solution

Well you know

We'd all love to see the plan

真の解決策を得たって君は言う

わかるだろ

みんなそのプランを見てみたい

tree-sitter CLI (0.25.0 or later)

これもwhichコマンドで確認できます。

which tree-sitter

Homebrewでインストールしている場合は Requiredとして、一緒にインストールされているはずです。

which-tar-curl

You ask me for a contribution

Well you know

We're all doin' what we can

僕に貢献を求めてくるけど

わかってるだろ

みんなできることをやっている

C compiler

わたしの経験で言えばmacOSでは問題になったことがありません。最低限Command Line Toolsが入っていれば大丈夫なはずです。 (例えばHomebrewのインストール時に自動で導入されます。)

Windowsの場合はやっぱり別途案内 がされているので、そちらを参照頂ければ...。

Linuxの場合、もしかしたら別途インストールが必要かもしれないので手っ取り早く解決方法だけ載っけちゃうんですが、 gcc-c++、もしくはclangをインストールするのが良さそうです。

gcc-c++gcc_cpp
clanggcc_cpp

Note

Readmeにも明記されているようにlibstdc++も必要になるはずなので、gccだとうまくいきませんでした😮

But if you want money for people with minds that hate

All I can tell you is brother you have to wait

だけどもし 憎しみに染まった者たちのためにカネが欲しいって話になるなら

僕が言えるのは 「なあ兄弟、君は待たなきゃいけない」 ってことだ

Node (23.0.0 or later) for some parsers

書いてあることそのままですが、"一部の" パーサーでは Node v23 以降を必要とします。

2025/06/05 時点では LTSバージョンが v22 らしいので、 場合に依っては なんか妙にハードルが高く感じられるかもしれません。

例えばNode.js®をダウンロードする に最初に示されている通りに進んでしまうとうまく行かない (かもしれない) ...😰

currentバージョンは v24 まで進んでいるので、単純に「brewapt を使った方が簡単だぞ❗」というのは簡単なんだけど...、 はっきり言って、私はここで責任を負わされたくありません 😤

もし必要になったら 乗り越えて❗」ぐらいで見逃してください...🥹

Note

よくわかんねー ってなっちゃう場合、ここはスキップして進みましょう 🐈

Don't you know it's gonna be

Alright, alright, alright

鈍調な日、こんな日

オーライ、オーライ! …オーライ?

Install

前項の確認さえ済めば、あとはpackerにお願いするだけで「あっ❗」と言う間に終わります😆

extensions/init.luaに以下を追記しましょう。

extensions/init.lua

require('packer').startup { function()
  use 'wbthomason/packer.nvim'

  -- 前節で入れたpackerと同列に並べる
  use {
    'nvim-treesitter/nvim-treesitter',
    branch = 'main',
    run = ':TSUpdate',
  }

end,

-- (以下略)

Warning

もし Neovim 0.11.0 より古いバージョンで使用するのであれば、branch を'master' に変えておいてね❗

-    branch = 'main',
+    branch = 'master',

そしたら :PackerSync を実行しましょう❗

installed

簡単ですね😉 すっごい古いスクリーンショットだから 見にくいけど❗

Note

オフィシャルに示されているのはlazy.nvimを使用した設定方法なのですが、 このサイトでは17章まではpackerを使用したサンプルコードを示しています。

(これも書き直した方がいいとは思ってるんだけど...😅)

Config

Neovimプラグインの場合、Readmeである程度デフォルト設定が示されていて、 それを基に「変える?変えない?」を決めるみたいな、 割とアバウトな方法にどうしてもなってくる...んじゃないかなぁと思ってるんですがどうでしょう❓

今回はもうデフォルト設定のままでいくので、何もする必要がありません❗

setup({opts})                                          *nvim-treesitter.setup()*

    Configure installation options. Needs to be specified before any
    installation operation.

    インストールオプションの設定。
    インストール操作の前に指定する必要があります。

    Note: You only need to call `setup` if you want to set non-default
    options!

    注意: `setup` を呼び出す必要があるのは、デフォルト以外のオプションを設定する場合だけです!

    Parameters: ~
    • {opts}  `(table?)` Optional parameters:
              • {install_dir} (`string?`, default `stdpath('data')/site/`)
                directory to install parsers and queries to. Note: will be
                prepended to |runtimepath|.

Note

以下の例はNeovimがまだパーサーを持っていなかった頃のスクリーンショットですが、 "適したパーサーを使用すると、こんな感じで色付けがされます" という例です。

defaultcolor1
nvim-treesittercolor2

You say you'll change the constitution

Well you know

We all wanna change your head

憲法を変えてやるって君は言う

まあ その通りだ

みんな "君のアタマ" を替えてやりたい

Commands

まず前提として、以下があります。

:h treesitter-parsers

PARSER FILES                                              *treesitter-parsers*

Parsers are the heart of treesitter. They are libraries that treesitter will
search for in the `parser` runtime directory.

Nvim includes these parsers:

パーサはtreesitterの心臓部です。これらは treesitter が `parser` ランタイムディレクトリで検索するライブラリです。
Nvimはこれらのパーサーを含んでいます:

- C
- Lua
- Markdown
- Vimscript
- Vimdoc
- Treesitter query files |ft-query-plugin|

You can install more parsers manually, or with a plugin like
https://github.com/nvim-treesitter/nvim-treesitter .

手動でさらにパーサーをインストールすることもでき、
https://github.com/nvim-treesitter/nvim-treesitter のようなプラグインを使うこともできます。

ということで、nvim-treesitterを使用してパーサーを管理するために使うコマンドが以下に示されています😉

:h nvim-treesitter-commands

COMMANDS                                              *nvim-treesitter-commands*

次項から、さらっとした使い方だけ示します。

You tell me it's the institution

Well you know

You better free your mind instead

君はそれを制度だって説く

けど そうじゃない

君は心を解放したほうがいい

TSInstall

:h TSInstall

:TSInstall {language}                                               *:TSInstall*

Install one or more treesitter parsers. {language} can be one or multiple
parsers or tiers (`stable`, `unstable`, or `all` (not recommended)). This is a
no-op of the parser(s) are already installed. Installation is performed
asynchronously. Use *:TSInstall!* to force installation even if a parser is
already installed.

1つ以上の treeitter パーサーをインストールします。
{language} には1つまたは複数のパーサーまたは階層 (`stable`、`unstable`、`all`(推奨しない)) を指定できます。
パーサがすでにインストールされている場合は、このオプションは無効です。
インストールは非同期に実行されます。
パーサーが既にインストールされている場合でも、強制的にインストールするには *:TSInstall!* を使用します。

language の部分は Supported languages に示されているものから選んで指定します。

The following is a list of languages for which a parser can be installed through :TSInstall.

以下は、:TSInstallでパーサをインストールできる言語のリストです。

...

例えば rustパーサーをインストールしたいなー😆 ってなったら以下のコマンドを使用します。

:TSInstall rust

TSInstallFromGrammar

:h TSInstallFromGrammar

:TSInstallFromGrammar {language}                         *:TSInstallFromGrammar*

Like |:TSInstall| but also regenerates the `parser.c` from the original
grammar. Useful for languages where the provided `parser.c` is outdated (e.g.,
uses a no longer supported ABI).

|:TSInstall| と似ていますが、`parser.c` を元の文法から再生成します。
提供された `parser.c` が古くなっている言語 (例えば、サポートされなくなった ABI を使用している場合など) に便利です。

あまり使う機会はないと思いますが、使い方は同じですね。

:TSInstallFromGrammar rust

TSUpdate

:h TSUpdate

:TSUpdate [{language}]                                              *:TSUpdate*

Update parsers to the `revision` specified in the manifest if this is newer
than the installed version. If {language} is specified, update the
corresponding parser or tier; otherwise update all installed parsers. This is
a no-op if all (specified) parsers are up to date.

Note: It is recommended to add this command as a build step in your plugin
manager.

マニフェストで指定された `revision` がインストールされているバージョンより新しい場合、パーサを更新します。
{language} が指定されている場合は、対応するパーサまたは階層を更新します。
そうでない場合は、インストールされているすべてのパーサを更新します。
指定された全てのパーサが最新である場合、これは省略されます。

Note: このコマンドをプラグインマネージャのビルドステップとして追加することを推奨します。

インストールされているパーサをアップデートしたいならこれ❗

:TSUpdate

TSUninstall

:h TSUninstall

:TSUninstall {language}                                           *:TSUninstall*

Deletes the parser for one or more {language}, or all parsers with `all`.

1つ以上の {language} のパーサを削除するか、`all` で全てのパーサを削除します。

インストールされているパーサを削除したいならこれ❗

:TSUninstall rust

TSLog

:h TSLog

:TSLog                                                                  *:TSLog*

Shows all messages from previous install, update, or uninstall operations.

以前のインストール、アップデート、アンインストール操作のすべてのメッセージを表示します。

nvim-treesitterで行った操作のログを確認したいならこれ❗

:TSLog

CheckHealth

これはnvim-treesitterに限らないNeovimの機能になりますが、healthチェックというものがあります😉

:h health

health.vim is a minimal framework to help users troubleshoot configuration and
any other environment conditions that a plugin might care about.

health.vim は、プラグイン設定やその他の環境条件の
トラブルシューティングを支援するための最小限のフレームワークです。

Plugin authors are encouraged to write new healthchecks. |health-dev|

プラグインの作者は新しいヘルスチェックを書くことが推奨されています。

コマンドは:h health-commandsにある通りです。試しに動かしてみましょう。

:che

または

:checkhealth
checkhealth

結果が表示されましたね☺️ これは すっごい古いスクリーンショット だけど❗

診断内容はプラグインに依りますが、 nvim-treesitterの場合は、依存ソフトウェアの確認と、OS情報・インストールされたパーサの表示を行ってくれます。

Note

これもヘルプそのままですが、指定したプラグインだけを診断することも可能です。

:che nvim-treesitter

とすると、nvim-treesitterのヘルスチェックのみを行えます。

Tip

冒頭の説明では環境条件と表されていますが、packerの節で少し触れた依存関係と (大体は) 同じ意味でしょう。 プラグインによっては、今回のようにヘルスチェックを提供してくれているので、困った時はこれも参考にすると良いです😉

But if you go carryin' pictures of Chairman Mao

You ain't gonna make it with anyone anyhow

肌身離さず 毛主席 の写真を持っていたって

どうせ誰とも上手くいかないんだから

Revolution / Miracle Gift Parade 💝

というわけで nvim-treesitter でした。

さて、ここまで来たら次にやることはもう決まってますね😉 カラーテーマです❗

Success

次回でついに瞳に優しく、そう❗生まれ変わるのです😆

Don't you know it's gonna be

Alright, alright, alright

鈍調な日、越えた日

オーライ、オーライ! …オーライ?