Syntax aware text-objects, select, move, swap, and peek support.
Warning: tree-sitter and nvim-treesitter are an experimental feature of nightly versions of Neovim. Please consider the experience with this plug-in as experimental until tree-sitter support in Neovim is stable! We recommend using the nightly builds of Neovim or the latest stable version.
You can install nvim-treesitter-textobjects with your favorite package manager, or using the default pack feature of Neovim!
If you are using vim-plug, put this in your init.vim file:
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
Plug 'nvim-treesitter/nvim-treesitter-textobjects'
Define your own text objects mappings
similar to ip
(inner paragraph) and ap
(a paragraph).
lua <<EOF
require'nvim-treesitter.configs'.setup {
textobjects = {
select = {
enable = true,
-- Automatically jump forward to textobj, similar to targets.vim
lookahead = true,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
},
-- You can choose the select mode (default is charwise 'v')
selection_modes = {
['@parameter.outer'] = 'v', -- charwise
['@function.outer'] = 'V', -- linewise
['@class.outer'] = '<c-v>', -- blockwise
},
-- If you set this to `true` (default is `false`) then any textobject is
-- extended to include preceding xor succeeding whitespace. Succeeding
-- whitespace has priority in order to act similarly to eg the built-in
-- `ap`.
include_surrounding_whitespace = true,
},
},
},
}
EOF
Define your own mappings to swap the node under the cursor with the next or previous one, like function parameters or arguments.
lua <<EOF
require'nvim-treesitter.configs'.setup {
textobjects = {
swap = {
enable = true,
swap_next = {
["<leader>a"] = "@parameter.inner",
},
swap_previous = {
["<leader>A"] = "@parameter.inner",
},
},
},
}
EOF
Define your own mappings to jump to the next or previous text object.
This is similar to ]m
, [m
, ]M
, [M
Neovim's mappings to jump to the next
or previous function.
lua <<EOF
require'nvim-treesitter.configs'.setup {
textobjects = {
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
},
},
}
EOF
lua <<EOF
require'nvim-treesitter.configs'.setup {
textobjects = {
lsp_interop = {
enable = true,
border = 'none',
peek_definition_code = {
["<leader>df"] = "@function.outer",
["<leader>dF"] = "@class.outer",
},
},
},
}
EOF
Textobjects are defined in the textobjects.scm
files.
You can extend or override those files by following the instructions at
https://github.com/nvim-treesitter/nvim-treesitter#adding-queries.
You can also use a custom capture for your own textobjects,
and use it in any of the textobject modules, for example:
-- after/queries/python/textobjects.scm
(function_definition) @custom-capture
lua <<EOF
require'nvim-treesitter.configs'.setup {
textobjects = {
select = {
enable = true,
keymaps = {
-- Your custom capture.
["aF"] = "@custom-capture",
-- Built-in captures.
["af"] = "@function.outer",
["if"] = "@function.inner",
},
},
},
}
EOF