Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

count the number of characters in a selected area

3,346 views
Skip to first unread message

Alex

unread,
Sep 5, 2008, 3:23:02 AM9/5/08
to
Hi all.

Is there an easy way to count the number of characters in a selected
area with VIM?
What I need is a way to select some text, press a key and have the
number of characters displayed somewhere in the VIM window.

Thanks in advance.

Alex

Ben C

unread,
Sep 5, 2008, 3:31:37 AM9/5/08
to

Select the text, press gCTRL-G, and all the information appears at the
bottom.

Almost what you want, but it tells you the number of bytes, not the
number of characters.

Alex

unread,
Sep 5, 2008, 4:56:47 AM9/5/08
to
On 5 Set, 09:31, Ben C <spams...@spam.eggs> wrote:

Thanks.
So you are saying that number of bytes is equal to number of
characters?

Ben C

unread,
Sep 5, 2008, 5:10:32 AM9/5/08
to
On 2008-09-05, Alex <metallo...@gmail.com> wrote:

It's the same if you're using only ASCII (Roman letters basically).

But if you're typing in Chinese, say, it will take at least two bytes to
represent each character. I'm not sure exactly what vim counts in that
case-- UTF-8 bytes possibly, even if fileencoding is set to latin1 (I
seem to get four bytes for two French accented characters with gCTRL-G
even if fileencoding=latin1). It may depend on the terminal you're
running vim in.

Patrick Texier

unread,
Sep 5, 2008, 5:59:28 AM9/5/08
to
Le Fri, 05 Sep 2008 04:10:32 -0500, Ben C a écrit :

> But if you're typing in Chinese, say, it will take at least two bytes to
> represent each character. I'm not sure exactly what vim counts in that
> case-- UTF-8 bytes possibly, even if fileencoding is set to latin1 (I
> seem to get four bytes for two French accented characters with gCTRL-G
> even if fileencoding=latin1). It may depend on the terminal you're
> running vim in.

Vim uses encoding, not fileencoding.
With enc=UFT-8, Vim counts bytes and characters.
--
Patrick Texier

vim:syntax=mail:ai:ts=4:et:tw=72

Ben C

unread,
Sep 5, 2008, 6:42:03 AM9/5/08
to
On 2008-09-05, Patrick Texier <p.te...@alussinan.org> wrote:
> Le Fri, 05 Sep 2008 04:10:32 -0500, Ben C a écrit :
>
>> But if you're typing in Chinese, say, it will take at least two bytes to
>> represent each character. I'm not sure exactly what vim counts in that
>> case-- UTF-8 bytes possibly, even if fileencoding is set to latin1 (I
>> seem to get four bytes for two French accented characters with gCTRL-G
>> even if fileencoding=latin1). It may depend on the terminal you're
>> running vim in.
>
> Vim uses encoding, not fileencoding.

I see, that explains it, thanks.

> With enc=UFT-8, Vim counts bytes and characters.

You're right. So OP just has to make sure encoding is set to utf-8.

Alex

unread,
Sep 6, 2008, 9:00:35 AM9/6/08
to
On Sep 5, 12:42 pm, Ben C <spams...@spam.eggs> wrote:

Humm, maybe I miss something, this does not work well for me:

1) :set encoding=UTF8
2) Type abc
3) Put the cursor on the first chararcter
3) Type gCTRL-G

Vim says: character 1 of 4

I need Vim to count exactly only the "visible" characters like
alphanumeric characters, punctuation white spaces. In a way that mimic
word count by some word processor like MS Office. Is that possible?

Stefan Weiss

unread,
Sep 6, 2008, 12:34:28 PM9/6/08
to
On 2008-09-06 15:00, Alex wrote:
> Humm, maybe I miss something, this does not work well for me:
>
> 1) :set encoding=UTF8
> 2) Type abc
> 3) Put the cursor on the first chararcter
> 3) Type gCTRL-G
>
> Vim says: character 1 of 4

Maybe I'm missing something now, but didn't you ask for a character
count in the selected area? You need to select something first (between
steps 3 and 3 in your list ;-)

stefan

Alex

unread,
Sep 7, 2008, 9:52:48 AM9/7/08
to
On Sep 6, 6:34 pm, Stefan Weiss

Uhmm.. you are right I asked for that ;-)
But that does not change the issue. If I select the first char and
tipe gCTRL-G Vim says: 1 of *4* characters.
So maybe there Vim count a hidden chars like carriage return or stuff
like that even if I did't press any other key after the last char. Is
it right?

Alex

Ben C

unread,
Sep 7, 2008, 12:43:39 PM9/7/08
to

Yes I think it's the newline. Vim always saves files with a newline at
the end.

Well, I say "always", I expect there's an option somewhere to turn that
off.

0 new messages