How to get character count using vi or vim
Solution 1
Use g CTRL-G
The output looks like
Col 1 of 5; Line 1 of 31; Word 1 of 48; Byte 1 of 571
Col 1 of 5;
refers to the position of the cursor on the line
you are on
Line 1 of 31;
refers to the line your cursor is currently on
Byte 1 of 571
refers to the character you are on (byte 1) vs the total bytes, or "characters" in the file (571)
UPDATE
Based on your expanded explanation, this should be what you need:
:go30
Also I'd like you to think of "characters" and use the term "bytes" -- It will make your future Google searches about vim
more fruitful.
Here is what it looks like on command line:
Solution 2
Count characters in just part of a file (or all of file if you need) First visualise the area you want to count.
- visualize a paragraph with v}
- visualize whole file with ggVG
then type
:%s/\%V./&/g
I needed this to count my twitter posts (280 character limit)
Related videos on Youtube
Ura718
Updated on June 04, 2022Comments
-
Ura718 almost 2 years
How do you get character location count from a text file using vi?
I tried ":goto number" and it does not work in Linux.
To clarify my question, if I have a file with say 3 lines:
I am going for a walk because today is a beautiful day.
I want to say find me a letter in position 30 and it will jump to line 2 highlighting letter 't' from word 'today'. This is similar to concept of
:goto 30
in macos vi but for Linux-
DJMcMayhem almost 6 years
:%s/.//gn<cr>
is one way. -
Christian Brabandt almost 6 years
:h wordcount()
-
-
Ura718 almost 6 yearsYour answer is interesting but not quiet what I was looking for. I updated my question to clarify further. Thanks!
-
Zak almost 6 yearsWhat about
:go30
-
Zak almost 6 yearsdo a
sudo apt-get install vim
to get the latestvim
with all the available commands -
Zak almost 6 yearsI am using Ubuntu 16.04 with
vim
Version 7.4.1689 -- do avi -v
to look at yourvim
version. -
Zak over 5 yearsTechnically my answer worked, -- Including the fact that I asked you to check your
VIM
version.and install the updated package if needs be -- Anyone who read my answer would have arrived at 1):goto 30
works .. And 2) if it didn't, check yourvim
version. Answering your own question was unnecessary. -
frabjous almost 5 yearsSince characters and bytes are not the same thing: e.g., if there are unicode multibyte characters, it would be nice to know how to do it for actual characters.
-
Peaceful over 2 yearsWhat is the explanation of the command that you suggested?
-
zzapper over 2 years\%V represents the visualised text, then replace every character '.' with itself '&' . This leaves text exactly the same but counts how many characters 'replaced'
-
jeffrey.d.m about 2 yearsRunning this command now returns
"Col 1 of 37; Line 1 of 586; Word 1 of 3557; Char 1 of 25042; Byte 1 of 25047"
, which addresses the unicode character issue :) @frabjous