Skip to content

Instantly share code, notes, and snippets.

@fuadsaud
Created June 27, 2025 10:42
Show Gist options
  • Save fuadsaud/06672e61a82a0de7c850a63b6bec9c6a to your computer and use it in GitHub Desktop.
Save fuadsaud/06672e61a82a0de7c850a63b6bec9c6a to your computer and use it in GitHub Desktop.
Incremental AST selection with Neovim and Treesitter

Something I really miss when working with non-Lisp languages is the ability to easily manipulate portions of code programatically.

Vim has, historically, provided text objects as a way to address that need. They're quite useful, but lack awareness of language-specific constructs—those would require a languag-aware parser.

I have recently learned that nvim-treesitter, being particularly well positioned to solve that problem (since it keeps the AST representation), provides some of that functionality natively:

require'nvim-treesitter.configs'.setup {
  incremental_selection = {
    enable = true,
    keymaps = {
      init_selection = "gnn", -- set to `false` to disable one of the mappings
      node_incremental = "grn",
      scope_incremental = "grc",
      node_decremental = "grm",
    },
  },
}

The default mappings might be a bit awkward. A user on reddit has suggested a different set of mappings that might be more convenient.

This setup has been making my life much easier when editing deeply nested TypeScript expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment