How do I visually select from the cursor to the matching bracket or parenthesis?
Answer
v%
Explanation
How it works
The % motion jumps to the matching bracket, parenthesis, or brace. When combined with visual mode (v), it selects everything from the cursor to the matching delimiter, including both delimiters.
Place your cursor on any opening or closing bracket character ((, ), [, ], {, }), then press v% to select the entire matched region.
% works with these pairs:
(and)[and]{and}- In some configurations,
<and>, and/*and*/
This differs from text objects like vi( or va( in an important way: v% requires the cursor to be on one of the bracket characters, while vi( works from anywhere inside the brackets. However, v% is useful when you want to include the brackets themselves and you are already positioned on one.
You can also use V% for linewise selection to the matching bracket, which is handy for selecting entire function bodies or blocks.
Example
Given this code with the cursor on the opening {:
function greet() {
console.log("hello");
return true;
}
- Place cursor on
{on line 1 - Press
v%to enter visual mode and jump to the matching} - Everything from
{to}(inclusive) is selected
You can then delete with d, yank with y, or perform any other operation on the selection. For linewise selection of the whole block, use V% instead.