vai
to select current context!
Context-aware smart indent object to select block, powered by treesitter.
This plugin is intended to be used with indent-blankline.nvim, so you see the scope before you select.
Use your favourite plugin manager to install.
This includes lazy-loading on keymaps. If you install like this, you can ignore every instruction below.
{
"kiyoon/treesitter-indent-object.nvim",
keys = {
{
"ai",
function() require'treesitter_indent_object.textobj'.select_indent_outer() end,
mode = {"x", "o"},
desc = "Select context-aware indent (outer)",
},
{
"aI",
function() require'treesitter_indent_object.textobj'.select_indent_outer(true) end,
mode = {"x", "o"},
desc = "Select context-aware indent (outer, line-wise)",
},
{
"ii",
function() require'treesitter_indent_object.textobj'.select_indent_inner() end,
mode = {"x", "o"},
desc = "Select context-aware indent (inner, partial range)",
},
{
"iI",
function() require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V') end,
mode = {"x", "o"},
desc = "Select context-aware indent (inner, entire range) in line-wise visual mode",
},
},
},
Click to see instructions for packer and vim-plug
-- init.lua
require("packer").startup(function()
use "nvim-treesitter/nvim-treesitter"
use "lukas-reineke/indent-blankline.nvim" -- optional
use "kiyoon/treesitter-indent-object.nvim"
end)
" init.vim
call plug#begin('~/.vim/plugged')
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'lukas-reineke/indent-blankline.nvim' " optional
Plug 'kiyoon/treesitter-indent-object.nvim'
call plug#end()
setup()
is not required, but here is the recommendation.
require("indent_blankline").setup {
show_current_context = true,
show_current_context_start = true,
}
-- Actually, no setup is required, but
-- if setup comes after the indent_blankline,
-- it will try to follow the pattern matching options
-- used in indent_blankline
require("treesitter_indent_object").setup()
Key bindings are not configured by default. Here are some examples.
" vai to select context-aware indent
xmap ai <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer()<CR>
omap ai <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer()<CR>
" vaI to ensure selecting entire line (or just use Vai)
xmap aI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer(true)<CR>
omap aI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer(true)<CR>
" vii to select inner block (only if block, only else block, etc.)
xmap ii <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner()<CR>
omap ii <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner()<CR>
" viI to select entire inner range (including if, else, etc.) in line-wise visual mode
xmap iI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V')<CR>
omap iI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V')<CR>
Lua equivalent:
-- select context-aware indent
vim.keymap.set({"x", "o"}, "ai", function() require'treesitter_indent_object.textobj'.select_indent_outer() end)
-- ensure selecting entire line (or just use Vai)
vim.keymap.set({"x", "o"}, "aI", function() require'treesitter_indent_object.textobj'.select_indent_outer(true) end)
-- select inner block (only if block, only else block, etc.)
vim.keymap.set({"x", "o"}, "ii", function() require'treesitter_indent_object.textobj'.select_indent_inner() end)
-- select entire inner range (including if, else, etc.) in line-wise visual mode
vim.keymap.set({"x", "o"}, "iI", function() require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V') end)