<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>book/src/guides/textobject.md - fork/helix @ master</title>
<meta name="description" content="book/src/guides/textobject.md in fork/helix at master.">
<meta property="og:title" content="book/src/guides/textobject.md - fork/helix @ master">
<meta property="og:description" content="book/src/guides/textobject.md in fork/helix at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="10f07d7eb089430930e6d3547a98b440b927d025">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/helix</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/helix</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">Björn Ganslandt</span><span class="commit-message" data-commit="2ee11a0a9d9b9951b5b64b54be05379cc030230b">Add textobjects for XML, HTML and JSX (#11158)</span><span class="snapshot-sha sha" data-commit="2ee11a0a9d9b9951b5b64b54be05379cc030230b">2ee11a0a</span><time datetime="2025-07-16T16:02:52+02:00" title="2025-07-16 16:02:52 +0200">2025-07-16</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a class="current" href="/fork/helix/ref/master/">master</a><strong>tags</strong><a href="/fork/helix/ref/23.05/">23.05</a><a href="/fork/helix/ref/23.10/">23.10</a><a href="/fork/helix/ref/24.03/">24.03</a><a href="/fork/helix/ref/24.07/">24.07</a><a href="/fork/helix/ref/25.01/">25.01</a><a href="/fork/helix/ref/25.01.1/">25.01.1</a><a href="/fork/helix/ref/25.07/">25.07</a><a href="/fork/helix/ref/25.07.1/">25.07.1</a><a class="all" href="/fork/helix/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/helix/">helix</a> / <a href="/fork/helix/ref/master/tree/book/">book</a> / <a href="/fork/helix/ref/master/tree/book/src/">src</a> / <a href="/fork/helix/ref/master/tree/book/src/guides/">guides</a> / <span class="cur">textobject.md</span></nav>
<span class="view-stats"><span>2.1 KiB</span><span>52 lines</span></span><span class="actions"><a class="raw" href="/fork/helix/raw/093129d4d00463a1e4d7f27429d40ba8e8663d3a/book/src/guides/textobject.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h2>Adding textobject queries</h2>
<p>Helix supports textobjects that are language specific, such as functions, classes, etc.
These textobjects require an accompanying tree-sitter grammar and a <code>textobjects.scm</code> query file
to work properly. Tree-sitter allows us to query the source code syntax tree
and capture specific parts of it. The queries are written in a lisp dialect.
More information on how to write queries can be found in the <a href="https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html">official tree-sitter
documentation</a>.</p>
<p>Query files should be placed in <code>runtime/queries/{language}/textobjects.scm</code>
when contributing to Helix. Note that to test the query files locally you should put
them under your local runtime directory (<code>~/.config/helix/runtime</code> on Linux
for example).</p>
<p>The following <a href="https://tree-sitter.github.io/tree-sitter/using-parsers/queries/2-operators.html#capturing-nodes">captures</a> are recognized:</p>
<table><thead><tr><th>Capture Name</th></tr></thead><tbody>
<tr><td><code>function.inside</code></td></tr>
<tr><td><code>function.around</code></td></tr>
<tr><td><code>class.inside</code></td></tr>
<tr><td><code>class.around</code></td></tr>
<tr><td><code>test.inside</code></td></tr>
<tr><td><code>test.around</code></td></tr>
<tr><td><code>parameter.inside</code></td></tr>
<tr><td><code>parameter.around</code></td></tr>
<tr><td><code>comment.inside</code></td></tr>
<tr><td><code>comment.around</code></td></tr>
<tr><td><code>entry.inside</code></td></tr>
<tr><td><code>entry.around</code></td></tr>
<tr><td><code>xml-element.inside</code></td></tr>
<tr><td><code>xml-element.around</code></td></tr>
</tbody></table>
<p><a href="https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+path%3A%2A%2A/textobjects.scm&amp;type=Code&amp;ref=advsearch&amp;l=&amp;l=">Example query files</a> can be found in the helix GitHub repository.</p>
<h2>Queries for textobject based navigation</h2>
<p>Tree-sitter based navigation in Helix is done using captures in the
following order:</p>
<ul>
<li><code>object.movement</code></li>
<li><code>object.around</code></li>
<li><code>object.inside</code></li>
</ul>
<p>For example if a <code>function.around</code> capture has been already defined for a language
in its <code>textobjects.scm</code> file, function navigation should also work automatically.
<code>function.movement</code> should be defined only if the node captured by <code>function.around</code>
doesn't make sense in a navigation context.</p>
</section><div class="code-panel"><pre class="code src language-markdown"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"><a-p>##</a-p> <a-tt>Adding textobject queries</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">Helix supports textobjects that are language specific, such as functions, classes, etc.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text">These textobjects require an accompanying tree-sitter grammar and a `textobjects.scm` query file</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text">to work properly. Tree-sitter allows us to query the source code syntax tree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text">and capture specific parts of it. The queries are written in a lisp dialect.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">More information on how to write queries can be found in the [official tree-sitter</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text">documentation][tree-sitter-queries].</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text">Query files should be placed in `runtime/queries/{language}/textobjects.scm`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">when contributing to Helix. Note that to test the query files locally you should put</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">them under your local runtime directory (`~/.config/helix/runtime` on Linux</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">for example).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text">The following [captures][tree-sitter-captures] are recognized:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">| Capture Name       |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">| ---                |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">| `function.inside`  |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text">| `function.around`  |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">| `class.inside`     |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text">| `class.around`     |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text">| `test.inside`      |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text">| `test.around`      |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text">| `parameter.inside` |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text">| `parameter.around` |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text">| `comment.inside`   |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text">| `comment.around`   |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text">| `entry.inside`     |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text">| `entry.around`     |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text">| `xml-element.inside` |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text">| `xml-element.around` |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text">[Example query files][textobject-examples] can be found in the helix GitHub repository.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text"><a-p>##</a-p> <a-tt>Queries for textobject based navigation</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text">Tree-sitter based navigation in Helix is done using captures in the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text">following order:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text"><a-p>- </a-p>`object.movement`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text"><a-p>- </a-p>`object.around`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text"><a-p>- </a-p>`object.inside`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text"><a-p>For example if a `function.around` capture has been already defined for a language</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text"><a-p>in its `textobjects.scm` file, function navigation should also work automatically.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text"><a-p>`function.movement` should be defined only if the node captured by `function.around`</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text"><a-p>doesn&#39;t make sense in a navigation context.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"><a-tu>[tree-sitter-queries]</a-tu><a-p>: </a-p><a-tu>https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html</a-tu></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text"><a-tu>[tree-sitter-captures]</a-tu><a-p>: </a-p><a-tu>https://tree-sitter.github.io/tree-sitter/using-parsers/queries/2-operators.html#capturing-nodes</a-tu></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"><a-tu>[textobject-examples]</a-tu><a-p>: </a-p><a-tu>https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+path%3A%2A%2A/textobjects.scm&amp;type=Code&amp;ref=advsearch&amp;l=&amp;l=</a-tu></span></span></pre></div></div>
</main>
</body>
</html>
