count the number of characters in a selected area

This is a discussion on count the number of characters in a selected area within the Editors forums in Theory and Concepts category; 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...

Go Back   Application Development Forum > Theory and Concepts > Editors

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-05-2008, 03:23 AM
Alex
Guest
 
Default count the number of characters in a selected area

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

Reply With Quote
  #2  
Old 09-05-2008, 03:31 AM
Ben C
Guest
 
Default Re: count the number of characters in a selected area

On 2008-09-05, Alex <metallourlante@gmail.com> wrote:
> 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.


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.
Reply With Quote
  #3  
Old 09-05-2008, 04:56 AM
Alex
Guest
 
Default Re: count the number of characters in a selected area

On 5 Set, 09:31, Ben C <spams...@spam.eggs> wrote:
> On 2008-09-05, Alex <metallourla...@gmail.com> wrote:
>
> > 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.

>
> 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.


Thanks.
So you are saying that number of bytes is equal to number of
characters?
Reply With Quote
  #4  
Old 09-05-2008, 05:10 AM
Ben C
Guest
 
Default Re: count the number of characters in a selected area

On 2008-09-05, Alex <metallourlante@gmail.com> wrote:
> On 5 Set, 09:31, Ben C <spams...@spam.eggs> wrote:
>> On 2008-09-05, Alex <metallourla...@gmail.com> wrote:
>>
>> > 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.

>>
>> 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.

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


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.
Reply With Quote
  #5  
Old 09-05-2008, 05:59 AM
Patrick Texier
Guest
 
Default Re: count the number of characters in a selected area

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
Reply With Quote
  #6  
Old 09-05-2008, 06:42 AM
Ben C
Guest
 
Default Re: count the number of characters in a selected area

On 2008-09-05, Patrick Texier <p.texier@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.
Reply With Quote
  #7  
Old 09-06-2008, 09:00 AM
Alex
Guest
 
Default Re: count the number of characters in a selected area

On Sep 5, 12:42 pm, Ben C <spams...@spam.eggs> wrote:
> On 2008-09-05, Patrick Texier <p.tex...@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.


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?
Reply With Quote
  #8  
Old 09-06-2008, 12:34 PM
Stefan Weiss
Guest
 
Default Re: count the number of characters in a selected area

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
Reply With Quote
  #9  
Old 09-07-2008, 09:52 AM
Alex
Guest
 
Default Re: count the number of characters in a selected area

On Sep 6, 6:34 pm, Stefan Weiss
<spaceman-4e64c-20080...@ausgehaucht.sensenmann.at> wrote:
> 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 ;-)


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
Reply With Quote
  #10  
Old 09-07-2008, 12:43 PM
Ben C
Guest
 
Default Re: count the number of characters in a selected area

On 2008-09-07, Alex <metallourlante@gmail.com> wrote:
> On Sep 6, 6:34 pm, Stefan Weiss
><spaceman-4e64c-20080...@ausgehaucht.sensenmann.at> wrote:
>> 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 ;-)

>
> 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?


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.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:21 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.