Big Files & Logs

How to count lines in a file on Mac

Note My already has the counts on screen, at the bottom of the window, updating as you type. Here is what each of the six counters measures, and how to count anything else you need.

You need a number. How many lines are in this export, how many characters are in this field, how many times does this identifier appear before you rename it. The usual answers involve a terminal command you have to look up, or a spreadsheet that mangles the file on the way in. If the file is open in an editor, the count should already be on screen.

In Note My it is. Six counters sit permanently in the status bar at the bottom of the window and update as you type. This guide covers what each one measures, how the occurrence counter answers, and how to count matches, lines and whole folders when the question grows beyond a single document.

The six counters in the status bar

The status bar carries six counter pills, left to right: Chars, Words, Spaces, Lines, Sel and Occ. Each has its own colour so you can find the one you want without reading the labels.

Six counters, live: characters, words, spaces, lines, the size of the selection, and how many times the selected word occurs. They are cached on the document, so a keystroke does not recount two million characters.

Nothing has to be switched on and there is no counting command to run. Open a file and the numbers are there. They read the document and never write to it, so a count can never change what is on disk.

The six status-bar counters and what each reports
CounterWhat it reports
CharsCharacters in the whole document.
WordsWords in the whole document.
SpacesSpace characters in the whole document.
LinesLines in the whole document.
SelHow much text is currently selected. It sits at zero with no selection.
OccHow many times the selected word occurs in the document.

The first four are document-wide and do not care where the caret is. The last two are about the selection, and they are the two worth understanding properly, because they follow rules.

Why the numbers are trustworthy on a big file

A counter is only useful if it is right, and staying right on a multi-megabyte file is the hard part. Our large-file engine keeps all six counters live on documents that a lighter editor would refuse to open. The numbers stay accurate, they keep pace with your typing, and they never make you wait.

Two things follow. The counts you read always belong to the document in front of you: switch tabs and the footer switches with you, with no leftover total from the file you just left. And on a file big enough to take a moment to open, the counters settle as soon as it is fully there, so the one habit worth keeping is to let a number stop moving before you quote it.

So you can treat the status bar as the answer rather than as a hint, whether you need a number to quote in a ticket or proof that an export came through whole.

All of this happens on your Mac. Counting a file does not upload it, there is no account to sign in to, and nothing about a document you open leaves the machine.

Sel — counting inside a selection

Select any run of text and the Sel pill reports its size. This is the quickest way to answer "how long is this block", "is this field over the limit", or "how much did I just paste", because you do not have to cut it out into a scratch document first. Deselect, and Sel returns to zero.

Reach for it whenever the limit lives outside the file: a form field with a maximum length, a message that has to fit, an excerpt someone asked you to keep short. Select, read, trim, select again.

Occ — how many times a word appears

Select a word and the Occ pill counts how many times it occurs in the document. Double-clicking is the usual way to select a word, and it also marks every other copy of that word in the editor, so you get the shape of the answer as well as the number. Any selection will do, though — drag across the word, or select it with the keyboard, and the pill answers just the same.

The pill keeps up while you move around, so Occ is something you can skim with. It is also the check to make before a rename: select the old name, read Occ, and you know how much of the file the change will touch.

What to select for the answer you want

Occ answers for a single plain word — letters, digits and underscores — which is exactly the shape of an identifier, a column name or a term you are about to rename. When the thing you want counted is longer than that, or has punctuation in it, our find and replace takes over and gives you a live total as you type. The table maps each question to the quickest route:

Which counter to reach for, by the kind of count you need
What you want to countHow to get the number
One word, anywhere in the documentDouble-click it. Occ reports the total immediately, and every other copy is marked in the editor so you can see where they sit.
A phrase, or several words togetherType it into Find & Replace. The match count updates with every keystroke.
Something with a bracket, a hyphen or a full stop in itFind & Replace again, with Whole Word or Regular Expression when you want the boundaries drawn tighter.
A long run of text, or a whole lineSelect it and read Sel for its size, then search for it in Find & Replace to see how often it recurs.

Occ waits for a selection before it answers, so double-click the word rather than parking the caret inside it and the number arrives at once. Deselect and it returns to zero, ready for the next word you pick.

Counting search matches instead

When the thing you want counted is not a plain word — a phrase, a pattern, a string with punctuation in it — count it with Find & Replace. Open it from the magnifying-glass button in the toolbar and type. The pop-over shows a running match count as you type, with Case Sensitive, Whole Word and Regular Expression options, and a scope switch between This File and All Tabs.

Note

The magnifying-glass button in the toolbar is the way into Find & Replace, and it sits in the same place in every window, so it is always one click away wherever you are working. ⌘F opens the system find bar for a quick look-up; the pop-over is where the live match count, the search options and the folder scope live.

For a count broken down by line rather than a single total, run Search ▸ Count Matches. It reports a HUD reading "N matches on M lines." — which is the number you want when a single line can contain the string several times and you actually care how many lines are affected.

One thing to know about Count Matches: it reports on the search you already have running, so type the query first and leave its highlights on screen. That is what makes it quick to iterate — refine the query, glance at the HUD, refine again — because the breakdown by line is one menu item away each time, with nothing to set up in between.

Counting the lines that contain a match

There is a second route to the same answer that leaves you with the lines themselves rather than a number. With a search active, press ⌥⌘B for Bookmark All Search Matches, then use Copy Bookmarked Lines from the same menu block and paste into a new tab. The Lines counter on the new tab is your count, and you now have the matching lines to read. F2 and ⇧F2 step through bookmarks, and ⌘F2 toggles one on the current line.

Finding the total line count without scrolling

Press ⌘L for Go to Line. The sheet reads "Enter a line number between 1 and N", where N is the last line in the document, and the field arrives pre-filled with the line you are on. It answers both "where am I" and "how long is this file" in one keystroke. A number outside that range is refused rather than accepted, so it will not quietly send you to the end.

Sharpening a query that matches everything

Find & Replace gives you an exact total, and when a query matches enormously often it says so in as many words, appending "(capped — narrow your query for full count)" to the status. You always know which kind of number you are looking at, which is the point: searching carries on, stepping through matches carries on, and nothing is quietly rounded off behind your back.

Read it as a prompt to sharpen the question. Turn on Whole Word, add case sensitivity, or write a regular expression that anchors the match to where it actually matters. A pattern with that many hits in one file is rarely the pattern you meant, and a tighter query hands you both an exact figure and a shorter list to read.

Counting across a whole folder

For a count that spans files rather than one document, use Search ▸ Find in Folder… (⇧⌘F). It searches every text file under a folder you choose and lists the results as they arrive, grouped by file, with each file node labelled "name (N matches)". Double-click a result to open that file at that line.

Glob masks narrow it to the files you care about, separated by ; or ,, and the skip-folders list defaults to .git; node_modules; build; .build; DerivedData so a search does not fill up with things you did not write. Match case, Whole word and Regular expression are all available.

The search reads your files and never modifies them, so you can point it at a whole project with nothing at stake. It is built to stay readable while it runs, and it tells you what it is doing at every point:

How a folder-wide search behaves on a big job
SituationWhat you get
A query that matches enormously oftenThe run tells you how far it got, so you always have a figure in hand. Narrow the query and search again for the exact total.
One file with a great many hitsResults stay spread across the folder, so a single noisy file cannot crowd out the rest. Open that file on its own for the full picture.
A very large fileGive it a tab of its own and search inside it, where our large-file engine keeps the six counters live and the match count moving as you type.
A very long lineThe preview beside each result stays to a readable length. Double-click the result to open the line in full.

Stopping a run early tells you where it got to: "Stopped — N matches in M files." A search with no hits says "No matches in N files.", which is itself a useful count.

Counting a very large document

Counting does not get less reliable as a file grows. Our large-file engine keeps the six counters live in a log of many millions of characters, and the window stays responsive while you scroll and type. That is worth knowing before you reach for a workaround: you do not need to split a big export into pieces, or sample the first part of a log and hope it is representative. Open the whole thing and read the totals off the footer.

Everything on this page holds at that size. The six counters read exactly as they do on a short note, Find & Replace answers the same questions and shows the same live total, Count Matches gives you the same breakdown by line, and ⌘L still names the last line number in the document. Nothing about the way you count changes because the file got bigger, which is the whole reason to open the real thing rather than a sample of it.

Four counts worth having to hand

The counters answer more questions than their labels suggest, once you know which one to reach for. Four that come up constantly:

  1. Rows in an export. A .csv file opens as plain text, so the Lines pill is your row count — take one off if the file carries a header. It is the quickest way to check that an export came through whole before anyone downstream relies on it.
  2. The size of a single field or message. Select the run of text and read Sel. Trim, select again, and watch the number come down to whatever limit you have been given, without cutting anything out into a scratch document.
  3. How much a rename will touch. Double-click the old identifier and read Occ. You know the scale of the change before you make it, and the marked copies show you where it lands.
  4. How many lines a pattern reaches. Run the search, then Search ▸ Count Matches. "N matches on M lines." separates a string that appears fifty times on two lines from one that appears fifty times across fifty.

When the number has to leave the app — into a ticket, a report, a message to whoever asked — search for the thing you are counting, press ⌥⌘B to bookmark every match, then use Copy Bookmarked Lines and paste into a new tab. The Lines pill on that tab is the count, and the matching lines sit underneath it as the evidence, in one piece of text you can send on.

Between them the six pills, our find and replace and the folder-wide search cover most of what people open a terminal for: how big, how many, how often, and where. All of it on your Mac, in the window you already have open.

Troubleshooting

Occ says 0 but the word is obviously there

Look at the selection rather than the document. Occ answers for one plain word, and a trailing space or a stray bracket caught by a drag changes what you have actually selected. Double-click the word instead of dragging across it: that snaps the selection to the word's own boundaries, and the number appears at once. For a phrase, or anything with punctuation inside it, put it into Find & Replace and read the live match count there.

Count Matches shows nothing

It reports on a search you are already running rather than starting one of its own. Run the search in the Find & Replace pop-over first, leave the highlights on screen, then choose Search ▸ Count Matches. Closing the pop-over clears the highlights, and with them the count.

The character count looks too low on a file that is still opening

A very large file takes a moment to open, and the counters settle as it does. Give it a beat before you trust a total: a number that has stopped rising is the final one, a number still climbing is not. If the figure is going into a ticket or a report, wait for it to hold steady, then read it.

The counts include text I have folded away

They do, and that is correct. Folding a block hides it from view; it does not take it out of the document. A folded file has exactly the same character, word and line counts as an unfolded one, which is what you want — otherwise a total would change every time you tidied the view.

My totals disagree with another tool's

Characters and lines will usually agree. Words and spaces are where tools differ, because "a word" is a definition, not a fact, and different programs draw the boundary in different places around punctuation, hyphens and numbers. If two counts must match, count something unambiguous — characters, or lines, or matches of an explicit pattern.

Frequently asked questions

How do I see a word count on a Mac without opening a terminal?

Open the file and read the status bar. The Words pill shows the count for the whole document and updates as you type. There is no command to run and nothing to enable.

How do I count how many times a word appears in a file?

Double-click the word. The Occ pill in the status bar reports how many times it occurs in the document. If the thing you want counted is a phrase or contains punctuation, use the Find & Replace pop-over from the toolbar instead and read its live match count.

How do I count how many lines contain a word, rather than how many times it occurs?

Run the search, then choose Search ▸ Count Matches. The HUD reads "N matches on M lines." — M is the line count you want.

Can I count lines in a very large file?

Yes. The counters work at any size. Our large-file engine keeps them accurate on documents of many millions of characters without stalling your typing, so there is no need to split a file up before you can count it. Open the whole export, let the numbers settle, and read the total off the footer.

Why does the match count say "capped"?

Because the query is matching enormously often, and Find & Replace tells you plainly rather than presenting a rounded figure as exact: it appends "(capped — narrow your query for full count)". Searching and stepping through the matches carry on as normal. Narrow the query with Whole Word, case sensitivity or a regular expression and the exact total comes back.

Does counting send the file anywhere?

Counting happens entirely on your Mac. Nothing is uploaded, there is no account to sign in to, and a file you open never leaves the machine — which means the counters work the same on a plane as they do at your desk.