How to get character count using vi or vim

16,515

Solution 1

Use g CTRL-G

The output looks like

Col 1 of 5; Line 1 of 31; Word 1 of 48; Byte 1 of 571

Col 1 of 5; refers to the position of the cursor on the line you are on

Line 1 of 31; refers to the line your cursor is currently on

Byte 1 of 571 refers to the character you are on (byte 1) vs the total bytes, or "characters" in the file (571)

UPDATE

Based on your expanded explanation, this should be what you need:

:go30

Also I'd like you to think of "characters" and use the term "bytes" -- It will make your future Google searches about vim more fruitful.

Here is what it looks like on command line:

:go30

Solution 2

Count characters in just part of a file (or all of file if you need) First visualise the area you want to count.

  1. visualize a paragraph with v}
  2. visualize whole file with ggVG

then type

:%s/\%V./&/g

I needed this to count my twitter posts (280 character limit)

Share:
16,515

Related videos on Youtube

Ura718
Author by

Ura718

Updated on June 04, 2022

Comments

  • Ura718
    Ura718 almost 2 years

    How do you get character location count from a text file using vi?

    I tried ":goto number" and it does not work in Linux.

    To clarify my question, if I have a file with say 3 lines:

    I am going for a walk 
    because today is a 
    beautiful day.
    

    I want to say find me a letter in position 30 and it will jump to line 2 highlighting letter 't' from word 'today'. This is similar to concept of :goto 30 in macos vi but for Linux

    • DJMcMayhem
      DJMcMayhem almost 6 years
      :%s/.//gn<cr> is one way.
    • Christian Brabandt
      Christian Brabandt almost 6 years
      :h wordcount()
  • Ura718
    Ura718 almost 6 years
    Your answer is interesting but not quiet what I was looking for. I updated my question to clarify further. Thanks!
  • Zak
    Zak almost 6 years
    What about :go30
  • Zak
    Zak almost 6 years
    do a sudo apt-get install vim to get the latest vim with all the available commands
  • Zak
    Zak almost 6 years
    I am using Ubuntu 16.04 with vim Version 7.4.1689 -- do a vi -v to look at your vim version.
  • Zak
    Zak over 5 years
    Technically my answer worked, -- Including the fact that I asked you to check your VIM version.and install the updated package if needs be -- Anyone who read my answer would have arrived at 1) :goto 30 works .. And 2) if it didn't, check your vim version. Answering your own question was unnecessary.
  • frabjous
    frabjous almost 5 years
    Since characters and bytes are not the same thing: e.g., if there are unicode multibyte characters, it would be nice to know how to do it for actual characters.
  • Peaceful
    Peaceful over 2 years
    What is the explanation of the command that you suggested?
  • zzapper
    zzapper over 2 years
    \%V represents the visualised text, then replace every character '.' with itself '&' . This leaves text exactly the same but counts how many characters 'replaced'
  • jeffrey.d.m
    jeffrey.d.m about 2 years
    Running this command now returns "Col 1 of 37; Line 1 of 586; Word 1 of 3557; Char 1 of 25042; Byte 1 of 25047", which addresses the unicode character issue :) @frabjous