VIM , how to show the count of occurrence the current match pattern under cursor? -
how show count of occurrence current match pattern under cursor ? example:
aa bb aab searching aa, cursor here, show 2. aa bb
and how insert number 2 after line . "aab" -> "aab 2"
here function , mapping job (add these lines end of .vimrc; needs @ least vim 7.4 , nocompatible
set before it):
nnoremap x :call count( '<c-r>=expand( '<cword>' )<cr>' )<cr> function! count( word ) redir => cnt silent exe '%s/' . a:word . '//n' redir end silent exe 's/.*/& ' . matchstr( cnt, '\d\+' ) . '/' endfunction
if pressing x
on word (bordered withespace characters), count
function add count of same words in file end of actual line.
to add ordinal number, change count
nthcount
in mapping , add these lines .vimrc:
function! nthcount( word ) redir => nth silent exe '0,.s/' . a:word . '//n' redir => cnt silent exe '%s/' . a:word . '//n' redir end silent exe 's/.*/& ' . matchstr( nth, '\d\+' ) . '/' echo a:word . ': ' . matchstr( nth, '\d\+' ) . '/' . matchstr( cnt, '\d\+' ) endfunction
for example pressing on first bb
:
aa bb 1 aab aa bb -- statusline -- bb: 1/2
Comments
Post a Comment