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.

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.
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.
:%s/guac//gn and ↵. The cursor is on the 0, so press r and then the number of matchesdojo.tsThis 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.
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?
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.
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?
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.
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?
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.
| Type | What 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. |