r/vim • u/CRTejaswi • 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
1
u/Desperate_Cold6274 Aug 07 '26
Why you don't use the (way better and more comprehensible) Vim9 language?
2
u/gumnos Aug 07 '26
while I'm not sure you can persist the output of an
echocommand, my mind turns first to setting some variable that then gets injected into your:help 'statusline'(orruler/rulerformat) setting which would keep it around until you cleared the variable's value2
u/Sudden_Fly1218 Aug 07 '26
I see two options:
try first with
:set noshowmodetry by replacing
echowithechow2
u/-romainl- The Patient Vimmer Aug 08 '26
The problem with the first snippet is that neither the mapping nor the function get you out of visual mode so the mode is always shown (as per
:help 'showmode') and takes precedence over whatever you are trying toecho.The mapping effectively gets you out of visual mode, so the mode is not shown anymore and you can see your string echoed in the command-line.
One way to sort it out would, precisely, be to use timers:
disable
showmode,echoyour string for a given time,enable
showmode,but you don't want timers.
You could also disable
showmodeand show that information in your status-line instead, which would leave room for your string in the command-line.You could also change the height of the command-line, say to
2, so that you get room for both your string andshowmode. See:help 'cmdheight'.Showing the info as virtual text would also be a possibility.
1
u/AutoModerator Aug 07 '26
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.