Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get character count using vi or vim

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

like image 433
Ura718 Avatar asked Jun 14 '18 16:06

Ura718


People also ask

How do you check word count in vi?

Counting number of words To count the number of words in the current buffer, press g then Ctrl-g.

How do I count the number of occurrences of a string in Vim?

This makes it easy to count the number of occurrences of the word under the cursor: first press * to search for the current word, then enter :%s///gn to count all occurrences of that word.

How would you find a character in vi?

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return.

How do I count the characters in a file?

To get exact character count of string, use printf, as opposed to echo, cat, or running wc -c directly on a file, because using echo, cat, etc will count a newline character, which will give you the amount of characters including the newline character.


1 Answers

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

like image 66
Zak Avatar answered Oct 17 '22 21:10

Zak