Key counter

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Key counter

C.G.Senthilkumar.

Is there a way vim can count the number of times a particular key
is pressed?

I'd like to improve the accuracy of my typing. For that, I'd like
to count the backspace usage while in edit mode. This counter should
reset automatically at the end of a session and should also be
resetable at will.

Thanks in advance.
Senthil.

--
Today's fortune:
Progress is impossible without change, and those who cannot change their
minds cannot change anything.
                 -- G.B. Shaw
Reply | Threaded
Open this post in threaded view
|

Re: Key counter

Tim Chase-2
> Is there a way vim can count the number of times a particular
> key is pressed?

Yep. :)  Watching a single key (like you wanted) is considerably
easier than watching *every* key.

> I'd like to improve the accuracy of my typing. For that, I'd
> like to count the backspace usage while in edit mode. This
> counter should reset automatically at the end of a session and
>  should also be resetable at will.

Something like the following should do what you describe:

        :let counter=0
        :inoremap <bs> <c-o>:let counter=counter+1<cr><bs>

(this assumes you don't use control+H as a backspace...however,
you can make a second mapping, mirroring the first one)

To find out how you're doing, simply

        :echo counter

This does just count the number of times you hit backspace, not
the number of changes, which can be slightly different, as if you
type a wrong letter, then a correct letter, and backspace
*twice* to correct the one *wrong* one, that's counted as two.

To reset the counter, just do the "let counter=0" again.

-tim





Reply | Threaded
Open this post in threaded view
|

Re: Key counter

C.G.Senthilkumar.

> :let counter=0
> :inoremap <bs> <c-o>:let counter=counter+1<cr><bs>
Thanks. It worked with a  instead of the last <bs>.

Now, would it be too much to ask for a running count to be displayed at some
place on the window? That is, something like line and column number being
displayed in the last line of the window.

Thanks in advance.
Senthil.
--
Today's fortune:
Progress is impossible without change, and those who cannot change their
minds cannot change anything.
                 -- G.B. Shaw
Reply | Threaded
Open this post in threaded view
|

RE: Key counter

Keith W. Roberts
 

> -----Original Message-----
> From: C.G.Senthilkumar. [mailto:[hidden email]]
> Sent: Friday, September 02, 2005 1:45 PM
> To: Tim Chase
> Cc: [hidden email]
> Subject: Re: Key counter
>
>
> > :let counter=0
> > :inoremap <bs> <c-o>:let counter=counter+1<cr><bs>
> Thanks. It worked with a  instead of the last <bs>.
>
> Now, would it be too much to ask for a running count to be
> displayed at some
> place on the window? That is, something like line and column
> number being
> displayed in the last line of the window.
>
> Thanks in advance.
> Senthil.
> --
> Today's fortune:
> Progress is impossible without change, and those who cannot
> change their
> minds cannot change anything.
>                  -- G.B. Shaw

:h 'stl'

You can insert {counter} into you statusline wherever you want it.

You also wanted to "reset" the counter per session.  This will depend how
you define "session", of course, but if you define it as "from the time you
enter Input mode until you leave it", then something like:

:inoremap <esc> <esc>:let counter=0<cr>

With these two things done, you will see the <bs> count while you are in
Input mode, and it'll be reset when you return to normal mode.  Personally,
I would do it differently: I would "reset" it only when entering input mode,
so instead of the above map, do:

nnoremap I :let counter=0<cr>I

(and the same for I, A, a, O, o, and any other way you use to enter Input
mode).

You could get even more sophisticated, and calculate stats on exiting Input
mode by adding to your .vimrc (untested):

inoremap <esc> <esc>:call TypingStats()
fun! TypingStats()
        " use substitute() so multi-byte chars are counted as one
        let nbr_chars = strlen(substitute(getreg('.'), ".", "x", "g"))
        echomsg "<BS> =" counter * 100 / nbr_chars . "%"
        " yada, yada, yada ...
endfun

Instead of echomsg, you could store stats in a register, or create a special
buffer, etc.  Have fun! :)

-Keith


Reply | Threaded
Open this post in threaded view
|

RE: Key counter

C.G.Senthilkumar.
> :h 'stl'
>
> You can insert {counter} into you statusline wherever you want it.
I'd rather include this in the 'ruler'. So, I did
:se rulerformat
$ grep rulerformat vimrc
etc., to find the default rulerformat and then edit it to include my stats.
But, I'm unable to find the default rulerformat for my vim. Where can I find
it?

I googled and found rulerformat=%20(%l,%c%V%=%5(%p%%%)%) in a mailing list
archive. At the outset, this seems to provide default behaviour. However,
being unable to verify this with any rc files, I'm afraid this might only
mimic a subset of the default behaviour.

Can someone verify if the above rulerformat is the default?

> You could get even more sophisticated, and calculate stats on exiting Input
> mode by adding to your .vimrc (untested):
>
> inoremap <esc> <esc>:call TypingStats()
> fun! TypingStats()
> " use substitute() so multi-byte chars are counted as one
> let nbr_chars = strlen(substitute(getreg('.'), ".", "x", "g"))
> echomsg "<BS> =" counter * 100 / nbr_chars . "%"
> " yada, yada, yada ...
> endfun
This is a good idea. So, I tried to have a running count of backspaces as a
percentage of the total key strokes in edit mode on my ruler.  But I couldn't
figure out a way to count the total # of keys pressed. Any ideas on how to do
this?


Thanks in advance.
Senthil.
--
Today's fortune:
1 + 1 = 3, for large values of 1.
Reply | Threaded
Open this post in threaded view
|

Re: Key counter

A.J.Mechelynck
----- Original Message -----
From: "C.G.Senthilkumar." <[hidden email]>
To: "Keith W. Roberts" <[hidden email]>
Cc: "'Tim Chase'" <[hidden email]>; <[hidden email]>
Sent: Monday, September 05, 2005 3:09 AM
Subject: RE: Key counter


>> :h 'stl'
>>
>> You can insert {counter} into you statusline wherever you want it.
> I'd rather include this in the 'ruler'. So, I did
> :se rulerformat
> $ grep rulerformat vimrc
> etc., to find the default rulerformat and then edit it to include my
> stats.
> But, I'm unable to find the default rulerformat for my vim. Where can I
> find
> it?
>
> I googled and found rulerformat=%20(%l,%c%V%=%5(%p%%%)%) in a mailing list
> archive. At the outset, this seems to provide default behaviour. However,
> being unable to verify this with any rc files, I'm afraid this might only
> mimic a subset of the default behaviour.
>
> Can someone verify if the above rulerformat is the default?
[...]

I don't know, but notice that (as mentioned under 'statusline') the
'rulerformat' will only be used in answer to the Ctrl-G normal-mode command
if 'laststatus' is 2; IOW, the 'rulerformat' is not guaranteed to be used
automatically. OTOH, whenever a status line is displayed (and in particular,
whenever split-window mode is used and whenever 'laststatus' is 2) the
status line _is_ guaranteed to be updated quite frequently and
automatically.


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

RE: Key counter

Keith W. Roberts
In reply to this post by C.G.Senthilkumar.
 

> -----Original Message-----
> From: C.G.Senthilkumar. [mailto:[hidden email]]
> Sent: Sunday, September 04, 2005 6:10 PM
> To: Keith W. Roberts
> Cc: 'Tim Chase'; [hidden email]
> Subject: RE: Key counter
>
> > :h 'stl'
> >
> > You can insert {counter} into you statusline wherever you want it.
> I'd rather include this in the 'ruler'. So, I did
> :se rulerformat
> $ grep rulerformat vimrc
> etc., to find the default rulerformat and then edit it to
> include my stats.
> But, I'm unable to find the default rulerformat for my vim.
> Where can I find
> it?
>
> I googled and found rulerformat=%20(%l,%c%V%=%5(%p%%%)%) in a
> mailing list
> archive. At the outset, this seems to provide default
> behaviour. However,
> being unable to verify this with any rc files, I'm afraid
> this might only
> mimic a subset of the default behaviour.
>
> Can someone verify if the above rulerformat is the default?
>
> > You could get even more sophisticated, and calculate stats
> on exiting Input
> > mode by adding to your .vimrc (untested):
> >
> > inoremap <esc> <esc>:call TypingStats()
> > fun! TypingStats()
> > " use substitute() so multi-byte chars are counted as one
> > let nbr_chars = strlen(substitute(getreg('.'), ".", "x", "g"))
> > echomsg "<BS> =" counter * 100 / nbr_chars . "%"
> > " yada, yada, yada ...
> > endfun
> This is a good idea. So, I tried to have a running count of
> backspaces as a
> percentage of the total key strokes in edit mode on my ruler.
>  But I couldn't
> figure out a way to count the total # of keys pressed. Any
> ideas on how to do this?

If you mean the total# of <bs> pressed, that was discussed earlier in the
thread, where you increment count (g:count) with an inoremap of <bs>.
Although I would choose something more descriptive, like g:bs_count.  If you
mean total# of keys pressed in Input mode, that's nbr_chars above.  Don't
forget, that function would be executed every time you "escape" from Input
mode, which is why you would use <c-o> to update g:bs_count (because you
only leave Input briefly, you haven't ended the whole Input).  This won't
even break existing maps which use the <esc>do_normal_mode_stuff_then_a|i
technique, as long as they were defined with inoremap. :)

Oops!  The map I gave was wrong:
        inoremap <esc> <esc>:call TypingStats()<cr>
It needs the <cr> to actually execute the Ex command, taking you back to
Normal mode when done.  [Otherwise it'll sit there waiting for a <cr>.]

Ok, so much for my original suggestion.  But now you want to keep a running
average on the ruler or statusline for every keystroke?  Not advisable.
Updating the total# of keys pressed would require updating a counter *on
every keypress*, and 2 of them on <bs>.  Very costly.  And IIRC, you'd have
to imap every single key to do it.

With my method, you wouldn't get any stats until Input is finished, but then
you shouldn't be looking for those stats while typing, anyway; it just slows
you down.  If you want intermediate stats, then define another inoremap as a
single keystroke which pops out of Input mode and updates the
counters/stats/whatever *without ending the input*, something like:
        inoremap <cr> <c-o>:call UpdateTypingStats()<cr>
and have this function recalculate the g:var which is returned by the
function in your ruler or statusline.  That way you'd get new stats for each
'hard' line entered (eg, at end of each paragraph).  Or create iabbrevs for
sentence endings which would update it at the end of every sentence.

IMO, statusline is best, because it is updated often; lots of things trigger
that.  I'm not sure about rulers; I don't use them.

-Keith

>
> Thanks in advance.
> Senthil.
> --
> Today's fortune:
> 1 + 1 = 3, for large values of 1.
>
>