Create your own textobjects using tree-sitter queries!
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'
Plug 'nvim-treesitter/nvim-treesitter-textobjects'If you want to use this plugin with Neovim 0.5, please use the 0.5-compat branches of this plugin and nvim-treesitter. Be aware though that most improvements will require neovim nightly.
Plug 'nvim-treesitter/nvim-treesitter', {'branch' : '0.5-compat'}
Plug 'nvim-treesitter/nvim-treesitter-textobjects', {'branch' : '0.5-compat'}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",
-- Or you can define your own textobjects like this
["iF"] = {
python = "(function_definition) @function",
cpp = "(function_definition) @function",
c = "(function_definition) @function",
java = "(method_declaration) @function",
},
},
},
},
}
EOFDefine 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",
},
},
},
}
EOFDefine 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- peek_definition_code: show textobject surrounding definition as determined using Neovim's built-in LSP in a floating window. Press the shortcut twice to enter the floating window (when neovim/neovim#12720 or its successor is merged)
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- @block.inner
- @block.outer
- @call.inner
- @call.outer
- @class.inner
- @class.outer
- @comment.outer
- @conditional.inner
- @conditional.outer
- @frame.inner
- @frame.outer
- @function.inner
- @function.outer
- @loop.inner
- @loop.outer
- @parameter.inner
- @parameter.outer
- @scopename.inner
- @statement.outer
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | |
| bash | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||||||||
| beancount | |||||||||||||||||||
| bibtex | 👍 | 👍 | |||||||||||||||||
| c | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| c_sharp | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| clojure | |||||||||||||||||||
| cmake | |||||||||||||||||||
| comment | |||||||||||||||||||
| commonlisp | |||||||||||||||||||
| cpp | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| css | |||||||||||||||||||
| cuda | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| d | |||||||||||||||||||
| dart | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| devicetree | |||||||||||||||||||
| dockerfile | |||||||||||||||||||
| dot | |||||||||||||||||||
| elixir | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||||||||||
| elm | |||||||||||||||||||
| erlang | |||||||||||||||||||
| fennel | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||
| fish | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||||||
| fortran | |||||||||||||||||||
| Godot (gdscript) | |||||||||||||||||||
| Glimmer and Ember | |||||||||||||||||||
| glsl | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| go | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||
| Godot Resources (gdresource) | |||||||||||||||||||
| gomod | |||||||||||||||||||
| graphql | |||||||||||||||||||
| haskell | |||||||||||||||||||
| hcl | |||||||||||||||||||
| heex | |||||||||||||||||||
| hjson | |||||||||||||||||||
| html | 👍 | 👍 | 👍 | 👍 | |||||||||||||||
| java | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||
| javascript | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||
| jsdoc | |||||||||||||||||||
| json | |||||||||||||||||||
| json5 | |||||||||||||||||||
| JSON with comments | |||||||||||||||||||
| julia | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||
| kotlin | |||||||||||||||||||
| latex | 👍 | 👍 | 👍 | 👍 | |||||||||||||||
| ledger | |||||||||||||||||||
| llvm | |||||||||||||||||||
| lua | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||||
| nix | |||||||||||||||||||
| ocaml | |||||||||||||||||||
| ocaml_interface | |||||||||||||||||||
| ocamllex | |||||||||||||||||||
| perl | |||||||||||||||||||
| php | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||||
| pioasm | |||||||||||||||||||
| python | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||
| ql | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||||||||||
| Tree-sitter query language | |||||||||||||||||||
| r | |||||||||||||||||||
| regex | |||||||||||||||||||
| rst | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||||||||
| ruby | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||||
| rust | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||
| scala | |||||||||||||||||||
| scss | |||||||||||||||||||
| sparql | |||||||||||||||||||
| supercollider | |||||||||||||||||||
| surface | |||||||||||||||||||
| svelte | |||||||||||||||||||
| swift | |||||||||||||||||||
| teal | |||||||||||||||||||
| tlaplus | |||||||||||||||||||
| toml | |||||||||||||||||||
| tsx | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||
| turtle | |||||||||||||||||||
| typescript | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | ||||||
| verilog | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | |||||||||||||
| vim | |||||||||||||||||||
| vue | 👍 | 👍 | 👍 | ||||||||||||||||
| yaml | |||||||||||||||||||
| yang | |||||||||||||||||||
| zig | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 | 👍 |