Learn Practice Daily golf Reference

How to count matches in Vim

Type :%s/guac//gn and press ↵. Vim reports something like 4 matches on 3 lines and changes nothing, because the n flag means count only. Put \< and \> around a word to count it only on its own.

The ninja standing beside a bowl of guacamole with a tally counter in one hand, clicking it once for each avocado on the counter, a sign above reading :%s//gn.

It's Friday, the pre-orders are in, and the kitchen wants to know how many avocados to cut. Every guac on the list takes one. Five orders you could count by eye, but the lunch list runs to fifty, and a miscount is how the guac runs out at one o'clock.

Vim counts for you with the substitute command. :%s/guac//gn reads as "on every line (%), substitute (s) guac with nothing, at every match on a line (g)", and the n at the end turns all of it into a count.

10 avocados to cut
1burrito, extra guac, salsa
22 burritos al pastor, salsa
3bowl, guac, no beans
4nachos, guac on the side, extra guac
5burrito, carnitas
NORMALLoading the editor…Ln 1, Col :1
Try it here

Avocados for Friday

Count the guac, then write the number over the 0 on the prep line. r replaces the character under the cursor with the next one you type.

Count with :%s/guac//gn and ↵. The cursor is on the 0, so press r and then the number of matchesdojo.ts
10 avocados to cut
1burrito, extra guac, salsa
22 burritos al pastor, salsa
3bowl, guac, no beans
4nachos, guac on the side, extra guac
5burrito, carnitas
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

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

How many lines have a match

The message gives two numbers. In 4 matches on 3 lines, the first is every guac in the file and the second is how many lines have at least one. The nachos order asks for guac twice, which is why they differ. So the avocados come from the first number, and the number of orders with guac from the second.

Leave out the g and Vim stops at the first match on each line, so :%s/guac//n counts each line once and both numbers come out the same.

Check yourself

Orders with guac

The cursor is at the top of the order list, in Normal mode. You type :%s/guac//n (no g) and press ↵. What does Vim say?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
10 avocados to cut
1burrito, extra guac, salsa
22 burritos al pastor, salsa
3bowl, guac, no beans
4nachos, guac on the side, extra guac
5burrito, carnitas
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Count a word only on its own

The pattern matches anywhere, even inside longer words. Before you rename the tip variable in the checkout code, :%s/tip//gn says 5 matches on 3 lines, but two of those are inside tipJar, which isn't changing.

Put \< before the word and \> after it, and Vim counts only the word standing on its own. The same goes for :%s/\<tip\>/gratuity/g when you do the rename, so count with the pattern you're going to use.

Check yourself

Just the tip

The cursor is at the start of the const tip line, in Normal mode. You type :%s/\<tip\>//gn and press ↵. What does Vim say?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1function orderTotal(order) {
2 const tip = order.tip ?? 0;
1 const tipJar = shop.tipJar;
2 return order.subtotal + tip;
3}
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Count the word under the cursor

When the word is already in front of you, skip the typing. Put the cursor on it and press *, which searches for that word on its own, \< and \> included, and jumps to the next one. Then type :%s///gn and press ↵. With nothing between the first two slashes, :s reuses the last search, so Vim counts the word you were on.

Check yourself

How many burritos?

The cursor is on burrito at the start of the first order. You press *, type :%s///gn and press ↵. What does Vim say?

BeforeJust a picture: pick an answer below (1, 2, 3)
10 avocados to cut
2burrito, extra guac, salsa
12 burritos al pastor, salsa
2bowl, guac, no beans
3nachos, guac on the side, extra guac
4burrito, carnitas
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

See the count while you search

Neovim keeps a running count as you search. After /guac and ↵, or n, N or *, the right end of the bottom line shows something like [2/4], meaning this is the second of four matches. Past 99 matches it shows [2/>99].

Vim can show the same counter but hides it by default. Type :set shortmess-=S to turn it on, or put set shortmess-=S in ~/.vimrc to keep it. The editor on this page doesn't show it, so use :%s///gn here.

TypeWhat Vim reports
:%s/guac//gn ↵Every guac in the file, and how many lines they're on.
:%s/guac//n ↵The lines with a guac (the first match on each line counts).
:%s/\<guac\>//gn ↵Only guac as a whole word, not guacamole.
:%s/guac//gni ↵Every guac, Guac and GUAC (i ignores case).
* then :%s///gn ↵The word under the cursor, as a whole word.
:'<,'>s/guac//gn ↵Only in the lines you selected (press : in Visual mode and Vim types the range).
:5,12s/guac//gn ↵Only in lines 5 to 12.
:%s/\S\+//gn ↵Every word in the file, counting anything between spaces as a word.
/guac ↵In Neovim, where you are among the matches, as [2/4]. In Vim after :set shortmess-=S.