Vim 打字时如何计数字符?

Vim 打字时如何计数字符?,vim,character,Vim,Character,我经常使用VIM在报纸或博客网站上写评论 通常输入的字符数最多 如何创建计数器(即状态栏中的计数器)以查看键入时键入的字符(包括空格)?设置允许使用%{…}特殊项计算表达式 因此,如果我们能找到一个表达式,返回当前缓冲区中的字符数(而不是字节数!),我们就可以将它合并到statusline中来解决这个问题 此命令执行以下操作: :set statusline+=\ %{strwidth(join(getline(1,'$'),'\ '))} 对于带有strwidth()的文本,不够好,因为它返

我经常使用VIM在报纸或博客网站上写评论

通常输入的字符数最多


如何创建计数器(即状态栏中的计数器)以查看键入时键入的字符(包括空格)?

设置允许使用
%{…}
特殊项计算表达式

因此,如果我们能找到一个表达式,返回当前缓冲区中的字符数(而不是字节数!),我们就可以将它合并到statusline中来解决这个问题

此命令执行以下操作:

:set statusline+=\ %{strwidth(join(getline(1,'$'),'\ '))}
对于带有strwidth()的文本,不够好,因为它返回的是显示单元格计数,而不是字符计数。如果双宽度字符是要求的一部分,请改用此改进版本:

:set statusline+=\ %{strlen(substitute(join(getline(1,'$'),'.'),'.','.','g'))}
但是请注意,表达式是在对缓冲区的每次更改时计算的


星期日下午奖金–光标下的字符位置也可以打包到单个表达式中。不适合胆小的人:

:set statusline+=\ %{strlen(substitute(join(add(getline(1,line('.')-1),strpart(getline('.'),0,col('.')-1)),'.'),'.','.','g'))+1}
通过混合和稍微修改代码,我为自己制作了以下内容,您可以将其放入
~/.vimrc
文件中(您需要有1秒的idol游标,以便函数计算单词和字符,并且可以通过修改
set updatetime=1000
更改值):

让g:word\u count=“”
设g:char_count=“”
函数WordCount()
返回g:word\u count
端功能
函数CharCount()
返回g:char\u计数
端功能
函数UpdateWordCount()
设lnum=1
设n=0

而lnum可以使用
gCTRL-G
查看缓冲区中有多少字节,如果使用ASCII,这与字符非常接近。您可以创建一个映射,以便在每次您(比如)离开插入模式或类似模式时查看该映射。@pandubear,是的,我知道,但我希望在键入时看到它(并且只显示字符数),非常好。非常感谢你!我发现这个也很有用:它一直计数到光标
strlen(替换(join(getline(1,,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
),但是当我将光标移到文本上时,它在正常模式下不工作。你知道为什么吗?@remann该表达式适用于缓冲区中的所有字符。我已经为光标下的字符添加了表达式–请参见我的编辑。非常感谢您周日下午的奖金!它工作得很好。
let g:word_count = "<unknown>"
let g:char_count = "<unknown>"
function WordCount()
    return g:word_count
endfunction
function CharCount()
    return g:char_count
endfunction
function UpdateWordCount()
    let lnum = 1
    let n = 0
    while lnum <= line('$')
        let n = n + len(split(getline(lnum)))
        let lnum = lnum + 1
    endwhile
    let g:word_count = n
    let g:char_count = strlen(substitute(join(getline(1,'$'),'.'),'.','.','g'))
endfunction
" Update the count when cursor is idle in command or insert mode.
" Update when idle for 1000 msec (default is 4000 msec).
set updatetime=1000
augroup WordCounter
    au! CursorHold,CursorHoldI * call UpdateWordCount()
augroup END
" Set statusline, shown here a piece at a time
highlight User1 ctermbg=green guibg=green ctermfg=black guifg=black
set statusline=%1*                        " Switch to User1 color highlight
set statusline+=%<%F                      " file name, cut if needed at start
set statusline+=%M                        " modified flag
set statusline+=%y                        " file type
set statusline+=%=                        " separator from left to right justified
set statusline+=\ %{WordCount()}\ words,
set statusline+=\ %{CharCount()}\ chars,
set statusline+=\ %l/%L\ lines,\ %P       " percentage through the file