Step 6 of 6

Tree-sitter text objects

Tree-sitter text objects

Brackets and quotes are only the beginning. When Helix knows the language of a file, it parses it with tree-sitter, and match mode gains text objects that describe code rather than characters:

  • f — a function
  • t — a type (a struct, enum, class, …)
  • a — an argument or parameter
  • c — a comment

They work exactly like the pairs: mif selects a function’s body, maf selects the whole function including its signature. The same letters also move you between objects — ]f selects the next function, [f the previous one, and the same goes for ]a, [t and the rest.

This sandbox opens every exercise as plain text, so Helix doesn’t know the buffer is Rust yet. Tell it with a command: type :lang rust and press Enter. The syntax lights up, and the tree-sitter objects come alive. Helix normally does this for you from the file extension.

Try it in the editor. Work through the checklist below to move around by function.

  1. Turn on Rust, then jump to the next function Type :lang rust and press Enter to set the language. Then, from anywhere inside main, type ]f.
  2. Select the function's body With the cursor inside add, type mif — only the block between the braces is selected.
  3. Select the whole function Type maf to select the signature as well.
  4. Jump back to the previous function Type [f to select main again.
Turn on Rust, then jump to the next function Type :lang rust and press Enter to set the language. Then, from anywhere inside main, type ]f.

Next: Select the function's body

Loading editor…