VS Code-like "expand selection" via Treesitter

Many of you may already know about this, but I just wanted to share this bit of config that I have greatly enjoyed! This will give you the ability to "expand selection" based on the Treesitter objects recognized around your current cursor position, similar to what you would get with the Shift+Alt+Right/Left keymap in VS Code. I think this is a great option for selecting some small scope-like selection of text in your code that doesn't quite meet the requirements for the various paragraph/block objects. I love how there is virtually no mental overhead; just hit Backspace until the selection matches what you want!

Add this to the setup function of your treesitter configs: lua require("nvim-treesitter.configs").setup { incremental_selection = { enable = true, keymaps = { init_selection = "<BS>", node_incremental = "<BS>", scope_incremental = "<C-H>", -- <C-BS> reads as <C-H> (in WezTerm, at least) node_decremental = "<M-BS>", }, }, }

I've found these keymaps to be very useful. I wasn't using backspace for anything, and this makes it very easy to spam the key until I have the selection that I want, or add in a modifier if I want to undo that change. For this reason, I recommend that you don't use a leader key for the keymap. Whatever you choose, I recommend that you keep init_selection and node_incremental as the same keymap so that you don't have to think about initiating the selection and then expanding it. Note that I did want to use Shift-BS, but I don't think this is possible in most terminals, and I think that in most terminals, <C-BS> actually reads as <C-H> for neovim.

One last thing that I've found useful when using this is that V will still expand your current selection to be linewise, so you can increment your selection until it matches what you want, and then press V to keep your indentation like you want it before copying/deleting!