Re: Count characters
Andy Wokula schrieb: 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 undo 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 Ok, thanks. This are a lot of cool Tips. But to complete the discussion maybe there is missing a command or script to count the characters without syntax words. For example for people who write LaTeX documents in vim and have to control theyr length... regards, Harry
Re: Count characters
Harald Kröll wrote: a command or script to count the characters without syntax words. For example for people who write LaTeX documents in vim and have to control their length... That depends on the definition of a control word. If you only want to exclude \backslash_prefixed \words, and nothing else, then this will do: :%s/\(\\\w*\)\@!\w//gn Explanation (right to left): //gncount ALL… \w …letters, numbers, and underscores… \@!…NOT preceded by… \( \\ \w* \)…a backslash and possibly other letters/numbers I'm not too familiar with Latex syntax, but if you wanted to exclude \this[kind of thing] as well, it wouldn't be hard: :%s/\v(\\\w*(\[[^]]*)?)@!\w//gn ( \\ \w* ( \[ [^]]* )? )@! \w Tobia
Re: Count characters
* Harald Kröll [2007.03.22 13:30]: Is there some function or script to count characters (letters without whitespaces) in vim? One way is to use the 'substitute' command's ability to tell you how many items it replaced. So if you type: :%s/\S//g You will get a message saying how many non-blank characters were found and replaced by themselves. -- JR
Re: Count characters
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 undo 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