30

How can I move the cursor to the nth byte in the buffer?

How can I display the current cursor position in the ruler in terms of the byte offset, rather than lines and columns? (Follow-up question: can it be displayed in both decimal and hexadecimal?)

1
  • Note that byte offset n and nth byte aren't the same thing. Byte 1 is at offset 0, byte 78 is at offset 77, etc.
    – jl6
    Nov 14, 2023 at 14:23

3 Answers 3

30
:goto 2356

jumps to the 2356th byte in the buffer.

Use the %o field in 'statusline' or 'rulerformat' to display the byte number of the character under your cursor.

Use %O to display the value in hexadecimal format.

Reference:

:help :goto
:help 'statusline'
:help 'rulerformat'
1
  • I found this only worked when opening the file in binary mode: vi -b <filename>
    – jl6
    Nov 14, 2023 at 14:23
11

You can hit g Ctrl+g and you get the full information about the position:

Col 13 of 35; Line 18 of 2272; Word 70 of 7767; Char 410 of 50021; Byte 410 of 50118
0
3

To display the byte position you can use the following command

:echo line2byte(line("."))+col(".")-1

See Question: Get offset of current buffer in vim

3
  • 1
    Probably, Vivian De Smedt forgot one ) in the end of line. Let me guess that he meant :echo(eval(line2byte(line("."))+col(".")-1)) Dec 9, 2020 at 8:55
  • 3
    I would rather recommend to use :echo eval(line2byte(line("."))+col(".")-1) (e.g. remove the very first parenthesis) Dec 9, 2020 at 9:16
  • 2
    @SamCenzored I've converted your answer to a comment, for now, since it seemed like you were clarifying a post (see vi.stackexchange.com/help/privileges/comment). Welcome to Vi and Vim!
    – D. Ben Knoble
    Dec 9, 2020 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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