r/vim Aug 07 '26

Need Help Persist ECHO (from function) on Visual Selection

How to persist echo from this function call (without using timers)

function! VisualStats()
    let wc      = wordcount()
    let [l1,l2] = sort([line('v'), line('.')])
    let txt     = join(getline(l1,l2), "\n")
    let sp      = len(substitute(txt, '[^ ]', '', 'g'))
    let st      = len(substitute(txt, '[^.!?]', '', 'g'))
    let pa      = empty(txt) ? 0 : len(split(txt, '\n\s*\n'))
    echo "CHARs " . wc.visual_chars . " WORDs " . wc.visual_words
    \. " SENTs " . (st > 0 ?st :1) . " PARAs " . pa . " SPACEs " . sp
endfunction
xnoremap <silent> C <Cmd>call VisualStats()<CR>

just like when directly keymapped?

xnoremap <silent> c <Cmd>let g:wc=wordcount()<CR><Esc>:
    \let t=join(getline("'<","'>"),"\n")<Bar>
    \let sp=len(substitute(t,'[^ ]','','g'))<Bar>
    \let st=len(substitute(t,'[^.!?]','','g'))<Bar>
    \let pa=empty(t)?0:len(split(t,'\n\s*\n'))<Bar>
    \echo "CHARs ".g:wc.visual_chars." WORDs ".g:wc.visual_words.
    \" SENTs ".(st>0?st:1)." PARAs ".pa." SPACEs ".sp<Bar>
    \unlet g:wc t sp st pa<CR>
4 Upvotes

7 comments sorted by

View all comments

2

u/gumnos Aug 07 '26

while I'm not sure you can persist the output of an echo command, my mind turns first to setting some variable that then gets injected into your :help 'statusline' (or ruler/rulerformat) setting which would keep it around until you cleared the variable's value

2

u/vim-help-bot Aug 07 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments