Count highlighted string length in VIM
Unless I'm missing something, Vim already does that. If I highlight some text using the mouse or by typing v
and moving the cursor, I see at the bottom of the screen
-- VISUAL -- 12
If you don't see this add set showcmd
to your .vimrc to enable it.
The number on the right is the number of highlighted characters. This only works if the selection is on a single row, otherwise it shows the row count.
You can also visually select some region of text and type g Ctrl-G which will show the number of lines, words and bytes selected.
Vim flips between displaying character count and line count for visualised text depending on what and how you visualise (vim 7.4 patched to Sept 2015)
V will display line count
v$ will display character count
If you visualise more than one line it only displays line count
g-CTRL-G displays 'byte count' which seems to be 'char count' +1 per line
:function VisualLength()
: exe 'normal "xy'
: echo "Visual: " . strlen(@x) . "\n"
: exe 'normal gv'
:endfunction
:map ,q "xy:call VisualLength()<CR>
First you yank the current selection (into buffer x)
Then you display the length of that buffer:
strlen(@x)
(The -- VISUAL -- displayed in the status line obscures this, so we have to add a newline)Highlight the previous visual range:
gv
This doesn't take account of whether the visual mode was line-, character- or block-mode, but it's enough for most cases.