Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wonder is there a way to count highlighted symbols in vim (including white spaces)?

UPDATE

VIM visual mode

It shows rows count if my selection is on multiple rows. I need symbols count.

share|improve this question
up vote 11 down vote accepted

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

where 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.

share|improve this answer
1  
(stunned) How the heck did I use Vim for so long and never notice that ? But in my defense, I was thinking about obtaining the number programmatically. – njd May 27 '11 at 8:44
    
Good answer, but take a look at updated question. As well as @njd I did not notice that. – Nemoden May 27 '11 at 9:28
    
@Nemoden: Try the second part of garyjohn's suggestion (g Ctrl-G). It should show a line like "Selected # of # Lines; # of # Words; # of # Bytes." – peth May 27 '11 at 9:48
    
Oh. My bad. I don't know why I've skipped the second solution. It works. Thank you! – Nemoden May 27 '11 at 9:50
4  
This isn't enabled by default on some systems. Just add set showcmd to your vimrc to enable it. – Tyler Holien Mar 14 '13 at 15:48
:function VisualLength()
:  exe 'normal "xy'
:  echo "Visual: " . strlen(@x) . "\n"
:  exe 'normal gv'
:endfunction

:map ,q "xy:call VisualLength()<CR>
  1. First you yank the current selection (into buffer x)

  2. 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)

  3. 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.

share|improve this answer

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
share|improve this answer
    
As the question says, even normal v switches from character-count to line-count if the selection spans multiple lines. However g CTRL-G does the job nicely. – joeytwiddle Feb 19 at 5:16

For a programmatic way within vimscript, this worked for me when the selection is limited to within one line:

let amount = virtcol("'>") - virtcol("'<")

(Using virtcol(..) instead of col(..), such that it works as expected when :set ve=all is active.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.