Member-only story

Neovim 101 —Tree-sitter Usage

Practical usage of Neovim Tree-sitter.

--

Previously, we went through the basics of Tree-sitter. In this article, let’s go through Tree-sitter usage to help us in coding.

This article is part of the Neovim 101 series.

The Neovim configuration files are available in this repository.

Getting Started

We already went through some of the practical usages of Tree-sitter in the Neovim Tips for a Betting Coding Experience series. This article covers additional tips not mentioned in the series.

Swapping

Previously we use the nvim-treesitter-textobjects plugin to swap parameters. Let’s extend this feature to other text objects.

In the lua/config/treesitter.lua file, we add the following code.

  • We define the key mappings to swap different text objects (line 1 — line 15)
  • We configure the swap mappings (line 25 — line 29)

Now we can swap classes.

  • <Alt-n><Alt-c> to swap with the next class
  • <Alt-p><Alt-c> to swap with the previous class
Swapping Classes

We can swap functions

  • <Alt-n><Alt-f> to swap with the next function
  • <Alt-p><Alt-f> to swap with the previous function
Swapping Functions

We can swap parameters

  • <Alt-n><Alt-p> to swap with the next parameter
  • <Alt-p><Alt-p> to swap with the previous parameter

--

--

alpha2phi
alpha2phi

Written by alpha2phi

Software engineer, Data Science and ML practitioner.

No responses yet