Function Vim文档中打印的字数
我想在我的.vimrc文件中添加一个函数来更新打开文档中的文本,特别是在找到文本“Word Count”的地方:它将使用vim在当前文档中插入准确的字数 这主要是一个编程练习,为了更好地学习vim,我知道有像wc这样的外部程序可以完成这项工作 下面是我用来计算代码行数的类似函数的示例:Function Vim文档中打印的字数,function,vim,Function,Vim,我想在我的.vimrc文件中添加一个函数来更新打开文档中的文本,特别是在找到文本“Word Count”的地方:它将使用vim在当前文档中插入准确的字数 这主要是一个编程练习,为了更好地学习vim,我知道有像wc这样的外部程序可以完成这项工作 下面是我用来计算代码行数的类似函数的示例: function! CountNonEmpty() let l = 1 let char_count = 0 while l <= line("$") if len(
function! CountNonEmpty()
let l = 1
let char_count = 0
while l <= line("$")
if len(substitute(getline(l), '\s', '', 'g')) > 3
let char_count += 1
endif
let l += 1
endwhile
return char_count
endfunction
function! LastModified()
if &modified
let save_cursor = getpos(".")
let n = min([15, line("$")])
keepjumps exe '1,' . n . 's#^\(.\{,10}LOC:\).*#\1' .
\ ' ' . CountNonEmpty() . '#e'
call histdel('search', -1)
call setpos('.', save_cursor)
endif
endfun
autocmd BufWritePre * call LastModified()
函数!CountNonEmpty()
设l=1
让char\u count=0
而l 3
让char_计数+=1
恩迪夫
设l+=1
循环结束
返回字符计数
端功能
功能!LastModified()
if&modified
让save_cursor=getpos(“.”)
设n=min([15,第行($))
KEEPEXE'1'。n.“s#^\(.{,10}位置:\).\35;\ 1'。
\ ' ' . CountNonEmpty()#e'
调用histdel('搜索',-1)
调用setpos('.',保存光标)
恩迪夫
结束
autocmd BufWritePre*调用LastModified()
有人能帮我找出如何添加到LastModified函数中,以便它在页眉中找到文本字数的地方插入一个字数吗?经过进一步挖掘,我找到了答案。这是另一个StackOverflow用户Michael Dunn的代码,发布在 我将在这里发布我是如何合并它的,以防其他人发现我的.vimrc的这一部分很有用:
function! CountNonEmpty()
let l = 1
let char_count = 0
while l <= line("$")
if len(substitute(getline(l), '\s', '', 'g')) > 3
let char_count += 1
endif
let l += 1
endwhile
return char_count
endfunction
function WordCount()
let s:old_status = v:statusmsg
exe "silent normal g\<c-g>"
let s:word_count = str2nr(split(v:statusmsg)[11])
let v:statusmsg = s:old_status
return s:word_count
endfunction
" If buffer modified, update any 'Last modified: ' in the first 20 lines.
" 'Last modified: ' can have up to 10 characters before (they are retained).
" Restores cursor and window position using save_cursor variable.
function! LastModified()
if &modified
let save_cursor = getpos(".")
let n = min([15, line("$")])
keepjumps exe '1,' . n . 's#^\(.\{,10}LOC:\).*#\1' .
\ ' ' . CountNonEmpty() . '#e'
keepjumps exe '1,' . n . 's#^\(.\{,10}Word Count:\).*#\1' .
\ ' ' . WordCount() . '#e'
call histdel('search', -1)
call setpos('.', save_cursor)
endif
endfun
autocmd BufWritePre * call LastModified()
函数!CountNonEmpty()
设l=1
让char\u count=0
而l 3
让char_计数+=1
恩迪夫
设l+=1
循环结束
返回字符计数
端功能
函数WordCount()
设s:old_status=v:statusmsg
exe“静默正常g\”
设s:word\u count=str2nr(split(v:statusmsg)[11])
设v:statusmsg=s:old_status
返回s:word\u count
端功能
如果缓冲区已修改,则更新前20行中的任何“Last modified:”。
“'Last modified:'之前最多可以有10个字符(保留)。
“使用save_cursor变量恢复光标和窗口位置。
函数!LastModified()
if&modified
让save_cursor=getpos(“.”)
设n=min([15,第行($))
keepjumps exe'1',.n.'s#^\(.\{,10}LOC:\).\35;\ 1'。
\''.CountNonEmpty()。#e'
KEEPEXE'1',.n.'s#^\(.\{,10}字数:\)..\35;\ 1'。
\''.WordCount()。#e'
调用histdel('搜索',-1)
调用setpos('.',保存光标)
恩迪夫
结束
autocmd BufWritePre*调用LastModified()