Tim Chase schrieb:
Is there some function or script to count characters (letters without
whitespaces) in vim?
For example Kile the Latex Editor has such a feature to control how
long texts are.
You can use
:%s/\w/&/g
which will report back "X substitutions on Y lines". "X" represents the
number of characters of interest. Adjust the "\w" regexp for whatever
constitutes your definiton of "characters" that you want to count.
Thus, this might be
:%s/\a/&/g "(only letters)
:%s/\S/&/g "(non-whitespace)
Or any other such combo.
It does have the side effect of "modifying" your document (setting the
"modified" flag). If this is a problem, you can "u"ndo it and it should
revert. It also requires that the document not be readonly (or at least
it will gripe if it is, warning you that you're changing a RO document).
If you have fewer than 'report' characters, Vim won't report back:
:help 'report'
but you can set this to
:set report=0
to always report any changes.
There are ways to do it on non-modifiable buffers, but they require a
bit more programatic logic, such as
:let x=0
:g/^/let x+=strlen(substitute(getline('.'), '\W', '', 'g'))
:echo x
where the '\W' is the inverse-set of characters of interest. In this
case, if you're interested in "\w" characters, the "\W" is the inverse.
If you're interested in non-whitespace characters ("\s"), you would use
"\S"; and if you're interested in counting vowels, you could use
"[^aeiouAEIOU]".
You might even notice that the second version uses a :g command that
matches every line. With this, you have a lot of flexibility:
:'<,'>g/^/let ... " counts characters in the linewise selection
:g/foo/let ... " counts characters on lines containing "foo"
and the like.
All sorts of fun things at your disposal :) Hope this helps,
-tim
Therefore in Vim7 the 'n' flag was added to the
substitute command:
:%s/\S/&/gn
just reports the number of matches. Works also
for read-only files, because no text is changed.
:h :s_flags
Andy
--
EOM
___________________________________________________________
Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de