Learn Practice Daily golf Reference

How to jump to the matching bracket in Vim

Put the cursor on a bracket, (, [ or {, and press % in Normal mode to jump to its partner. Press % again to jump back.

Try it here

Drop the debug options

The cursor is on a comma, not a bracket. d% still works: % finds the next bracket on the line, the {, so d% deletes from the comma to its }. Then do the same on the next line, with . to repeat it.

1 of 2Delete from the comma to the matching } with d%
1async function loadUser(id) {
2 const user = await fetchUser(id, { debug: true });
1 const orders = await fetchOrders(id, { debug: true });
2 return { user, orders };
3}
NORMALLoading the editor…1/20.0s2:34

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
% on a bracketJumps to its partner: ( and ), [ and ], { and }, across lines too.
% off a bracketFinds the first bracket after the cursor on the same line and jumps to that one's partner.
d%Deletes from the cursor to the partner, both brackets included.
v% / y% / c%Selects / copies / changes the same stretch.
[( / [{Goes back to the ( or { that the cursor is inside.
]) / ]}Goes forward to the ) or } that closes it.
50%With a count, % means a percentage: halfway down the file.
:set matchpairs+=<:> ↵Adds < and > to the pairs that % knows.
1async function loadUser(id) {
1 const user = await fetchUser(id, { debug: true });
2 const orders = await fetchOrders(id, { debug: true });
3 return { user, orders };
4}
NORMALLoading the editor…1:1

Inside a block, not on its bracket

% needs a bracket on the cursor's line. When you are somewhere inside a long block, [{ goes back to the { that opens it and ]} forward to the } that closes it; [( and ]) do the same for parentheses. They skip pairs that are already closed, so from anywhere in a function body [{ finds the function's opening brace, then % takes you to its end.

To act on the whole contents rather than travel, text objects are often shorter: di( deletes everything inside the parentheses around the cursor, and da{ deletes the braces too.

HTML tags, if and else: matchit

Out of the box, % matches brackets (and C comments and #if/#endif). The matchit plugin teaches it more pairs, defined per language by the filetype plugin: an HTML opening tag and its closing tag, if, else and fi in shell scripts, if and endif in Vim script. Neovim loads matchit by default. Vim ships it as an optional package: add packadd! matchit to ~/.vimrc, with filetype plugin on so the pairs are defined.