# Methods of counting characters on various systems

Counting characters of a program manually is tedious, and we do this a lot in Code Golf. How do you do a more effective character count using utilities provided by your shell or ide?

• (I know the answer, but wanted to try such a question to see if we consider this on-topic.) – moinudin Jan 27 '11 at 22:19
• Additionally should we include the counts of special compiler/interpreter flags in the character counts? – JPvdMerwe Jan 27 '11 at 22:22
• @JPvdMerwe That's a good question, but ask that on meta where we're fleshing out the description and usage of this site. – moinudin Jan 27 '11 at 22:25
• @Close voters: Since we're defining this site, can you please give reasons as to why you think should be off-topic. This post was experimental in that regard, but no feedback makes it a pointless exercise. – moinudin Jan 27 '11 at 22:42
• It's either meta.codegolf.stackexchange.com or superuser.com, because there is no puzzle. – Nakilon Jan 27 '11 at 23:16
• @Nakilon The area51 proposal had at least one meta-ish on-topic question: "Is code golfing the practice of elegance or obfuscation?" I disagree that this is for SU, as we've seen in answers there are specific details around newlines (and probably unicode too). – moinudin Jan 27 '11 at 23:21
• @Nakilon See meta.codegolf.stackexchange.com/questions/27/… – moinudin Jan 27 '11 at 23:25
• @marcog, if you asked this (about counting lines) question at proposal stage, I think, we could see mostly off-topic votes. – Nakilon Jan 27 '11 at 23:46
• @Nakilon Unfortunately I missed the definition stage, which is why I'm asking it now. – moinudin Jan 27 '11 at 23:49
• @Nakilon: The question is whether meta is "meta-puzzle/golf" or "meta-this-site" or both. I saw it more as "meta-this-site" so far and "meta-golf" questions would then be on scope. Just my opinion, though. – Joey Jan 31 '11 at 12:40
• @Joey Same opinion I voiced here. – moinudin Jan 31 '11 at 13:00

On Linux, use wc -c file in your favourite shell. Note that you might be counting a trailing new-line, which you might want to ignore.

• Any unix, not just linux. And you can ignore the -c if you're going to parse the output by Mk. I eyeball. – dmckee --- ex-moderator kitten Jan 27 '11 at 22:33

Notepad++ includes a convenient count of both characters (not including newlines) and bytes (be sure to first remove any trailing newlines and switch to Unix newlines) at the bottom of the window. Separate counts exist for the current selection and the entire file.

• That's probably what I'll use for most of my solutions. If I ever do one, that is. – Andy Feb 2 '11 at 4:42

I use this tool: http://www.javascriptkit.com/script/script2/charcount.shtml.

Usually I use one of these two ways:

1. I always have my Total Commander (FAR, MC, what do you wish) opened at current working directory, so I just look on file size in bytes and substract (number_of_lines - 1) if I used CR LF;
2. If I'm too lazy to open Total, I go to IRC and give solution code to bot to count it's length.
• Shift+F2, U, Enter in Far to save it with LF line endings. :-) – Joey Jan 31 '11 at 12:31

I just look at the character count when I open it in vim to copy across.

• Vim appends one unnecessary new line at the end of file. – Alexandru Jan 28 '11 at 21:30
• @Alexandru: :set binary noeol – ninjalj Feb 4 '11 at 22:14

For most golfings I just use Far, i.e. an orthodox file manager, save the file with LF line endings and just look at the file size (I need to remember svn ps svn:eolstyle LF * though because SVN tends to think too much). Since I'm on Windows there's luckily no trailing LF to be accounted for.

However, for one-liners in PowerShell, I have a small utility (slightly buggy, but never got around fixing them):

function Set-GolfPrompt {
function global:prompt {
# Info about how much shorter/longer the previous command was can only
# be given if the history is long enough, so don't do that if not.
$history = Get-History if ($history.Length -ge 2) {
$last = (Get-History)[-1].CommandLine.Length$2ndlast = (Get-History)[-2].CommandLine.Length

Write-Host -NoNewline -ForegroundColor DarkGray "Last command's length: $last " if ($2ndlast -eq $last) { Write-Host -NoNewline -ForegroundColor Gray "(?0)" } elseif ($2ndlast -lt $last) { Write-Host -NoNewline -ForegroundColor Red "(+$($last -$2ndlast))"
} else {
Write-Host -NoNewline -ForegroundColor Green "(-$($2ndlast - $last))" } } [Console]::Write("n ") 0..10 | ForEach-Object { [Console]::ForegroundColor = ($_+1) % 2 * 2 + 12
[Console]::Write("$_ ") } [Console]::Write("n ") 1..11 | ForEach-Object { [Console]::ForegroundColor =$_ % 2 * 2 + 12
[Console]::Write('1234567890')
}
[Console]::ForegroundColor = 'White'
[Console]::WriteLine()

"> "
}


Which then looks like this:

I like to paste the code into charactercountonline.com . I especially enjoy that I don't need to include the whole file in the count, because I often include useless information that doesn't need to be counted (e.g. in Java: package codegolf;).

I usually use Sublime for counting (even if I'm not writing the code in it). I paste the code I want to count into a new file. Then, if I'm sure that my code still works without any whitespace whatsoever, I prepare it with

• Ctrl +H (open Find & Replace)
• (optionally) Alt + R (activate regex search)
• \ s (search for whitespace)
• (optionally) Tab Del (clear replacement string)
• Ctrl + Alt + Enter (replace all)
• Ctrl + A (select all)

Now in the status bar at the bottom you'll see "x characters selected". This sequence of key presses becomes really mechanical after a while, and when you're doing it repeatedly, because you've been golfing more in the meantime, you can actually skip steps 2 to 4. Of course, some of those combinations may be configured differently on your machine, but I think they are the default key bindings in Windows.

This is particularly useful for Mathematica answers, because if you paste stuff out of Mathematica, you get tons of unwanted whitespace.

• And how do you count bytes for Mma programs? – Dr. belisarius Oct 10 '14 at 3:57
• @belisarius If I've got Unicode (in any language), after the above steps, I'll paste it into this online byte counter. Of course, in Mathematica in particular, you sometimes need to insert the Unicode characters by hand, because when copying out of Mathematica, it will replace stuff like ⋂ with \[Intersection]. – Martin Ender Oct 10 '14 at 8:42
• Yeah, thanks a lot. That char replacement is what sometimes drives me nuts. BTW, I count chars with Sublime too :) – Dr. belisarius Oct 10 '14 at 8:59
$python >>> len("".join(open('filename').readlines()))  or, as hallvabo suggests: $ python

• This would count the number of lines, not characters. – hallvabo Jan 31 '11 at 21:50
• @hallvabo, Good point. I've fixed it. – Thomas O Feb 1 '11 at 10:15
• Why not just open('filename').read()? And to avoid any behind-the-scenes automagial line terminator translation, use 'rb' as mode flag. – hallvabo Feb 1 '11 at 12:44
• @hallvabo, I didn't know you could do that. – Thomas O Feb 1 '11 at 12:45

Usually I am using vim, so for one liners I position the cursor on the last character and read off that position

For multiple lines, I save the file and subtract 1 from the reported size (because of the extra newline)

If I solve a problem in one line in the python interpreter, I'll just paste it into

>>> len("""<solution>""")


(I don't think I've ever had an answer with """ in it :))

• on vim: g <Ctrl>-g – ninjalj Feb 4 '11 at 22:15