Mit der Methode in der Antwort von Steve Moyer konnte ich die folgende Lösung herstellen. Es ist ein ziemlich uneleganter Hack, fürchte ich, und ich glaube, dass es eine bessere Lösung geben muss, aber es funktioniert und ist viel schneller als einfach alle Wörter in einem Puffer zu zählen, jedes Mal wenn die Statuszeile aktualisiert wird. Ich sollte auch beachten, dass diese Lösung plattformunabhängig ist und nicht davon ausgeht, dass ein System "wc" oder etwas Ähnliches hat.
Meine Lösung aktualisiert den Puffer nicht regelmäßig, aber die Antwort von Mikael Jansson könnte diese Funktionalität bereitstellen. Ich habe noch keine Instanz gefunden, bei der meine Lösung nicht mehr synchron ist. Ich habe dies jedoch nur kurz getestet, da eine genaue Anzahl von Live-Wörtern für meine Bedürfnisse nicht wesentlich ist. Das Muster, das ich für übereinstimmende Wörter verwende, ist ebenfalls einfach und für einfache Textdokumente gedacht. Wenn jemand eine bessere Idee für ein Muster oder andere Vorschläge hat, bitte zögern Sie nicht, eine Antwort zu posten oder diesen Beitrag zu bearbeiten.
Meine Lösung:
"returns the count of how many words are in the entire file excluding the current line
"updates the buffer variable Global_Word_Count to reflect this
fu! OtherLineWordCount()
let data = []
"get lines above and below current line unless current line is first or last
if line(".") > 1
let data = getline(1, line(".")-1)
endif
if line(".") < line("$")
let data = data + getline(line(".")+1, "$")
endif
let count_words = 0
let pattern = "\\<\\(\\w\\|-\\|'\\)\\+\\>"
for str in data
let count_words = count_words + NumPatternsInString(str, pattern)
endfor
let b:Global_Word_Count = count_words
return count_words
endf
"returns the word count for the current line
"updates the buffer variable Current_Line_Number
"updates the buffer variable Current_Line_Word_Count
fu! CurrentLineWordCount()
if b:Current_Line_Number != line(".") "if the line number has changed then add old count
let b:Global_Word_Count = b:Global_Word_Count + b:Current_Line_Word_Count
endif
"calculate number of words on current line
let line = getline(".")
let pattern = "\\<\\(\\w\\|-\\|'\\)\\+\\>"
let count_words = NumPatternsInString(line, pattern)
let b:Current_Line_Word_Count = count_words "update buffer variable with current line count
if b:Current_Line_Number != line(".") "if the line number has changed then subtract current line count
let b:Global_Word_Count = b:Global_Word_Count - b:Current_Line_Word_Count
endif
let b:Current_Line_Number = line(".") "update buffer variable with current line number
return count_words
endf
"returns the word count for the entire file using variables defined in other procedures
"this is the function that is called repeatedly and controls the other word
"count functions.
fu! WordCount()
if exists("b:Global_Word_Count") == 0
let b:Global_Word_Count = 0
let b:Current_Line_Word_Count = 0
let b:Current_Line_Number = line(".")
call OtherLineWordCount()
endif
call CurrentLineWordCount()
return b:Global_Word_Count + b:Current_Line_Word_Count
endf
"returns the number of patterns found in a string
fu! NumPatternsInString(str, pat)
let i = 0
let num = -1
while i != -1
let num = num + 1
let i = matchend(a:str, a:pat, i)
endwhile
return num
endf
Dies wird dann in der Statuszeile hinzugefügt von:
:set statusline=wc:%{WordCount()}
Ich hoffe, das jemand für eine Live-Wortanzahl in Vim sucht hilft. Wenn auch nicht immer genau.Alternativ bietet Ihnen g ctrl-g natürlich auch die Wortanzahl von Vim!
Was ist Ihre aktuelle Funktion? –
Für andere, die für eine allgemeine Wortzählung hierher kommen, benutzen Sie 'g Ctrl-g'. – naught101