treesitter-unit
A tiny Neovim plugin to deal with treesitter units. A unit is defined as a treesitter node including all its children. It allows you to quickly select, yank, delete or replace language-specific ranges.
For inner selections, the main node under the cursor is selected. For outer selections, the next node is selected.
Installation
Requirements: nvim-treesitter including a parser for your language
For vim-plug:
Plug 'David-Kunz/treesitter-unit'
For packer:
use 'David-Kunz/treesitter-unit'
Usage
Select treesitter unit
You can select the current treesitter unit
:lua require"treesitter-unit".select(outer?)
This function takes an optional Boolean flag to specify if the outer scope should be selected as well, default false
.
For operations like delete, change, ... please see section "Useful mappings".
Automatic Highlighting
You can toggle automatic highlighting for the current treesitter unit.
:lua require"treesitter-unit".toggle_highlighting(higroup?)
As an optional parameter you can specify the highlight group, default: "CursorLine"
.
Alternative: enable_highlighting(higroup?)
and disable_highlighting()
.
Useful mappings
For init.vim:
xnoremap iu :lua require"treesitter-unit".select()<CR>
xnoremap au :lua require"treesitter-unit".select(true)<CR>
onoremap iu :<c-u>lua require"treesitter-unit".select()<CR>
onoremap au :<c-u>lua require"treesitter-unit".select(true)<CR>
For init.lua:
vim.api.nvim_set_keymap('x', 'iu', ':lua require"treesitter-unit".select()<CR>', {noremap=true})
vim.api.nvim_set_keymap('x', 'au', ':lua require"treesitter-unit".select(true)<CR>', {noremap=true})
vim.api.nvim_set_keymap('o', 'iu', ':<c-u>lua require"treesitter-unit".select()<CR>', {noremap=true})
vim.api.nvim_set_keymap('o', 'au', ':<c-u>lua require"treesitter-unit".select(true)<CR>', {noremap=true})
Examples:
viu
to select the inner unitcau
to change the outer unit
Similar plugins
- nvim-treesitter-textobjects for more fine-granular control
- nvim-treesitter for incremental selection
- nvim-ts-hint-textobject