Jump to byte address in vim?
Asked Answered
R

3

49

I'm investigating a mainly UTF-8 file with lot of long lines. However, the file is not entirely text file, there is some garbage. To find my point of interest I'm using hd and grep.

So at some point I know it'm interested in e.g. 0000301a, so I want to quickly open the file in Vim and jump to that position.

Example (actually a tiny file, here the position is 0000001c):

me@here:~$ hd file | grep -C 10 \ 00\ 
00000000  6c 69 6e 65 31 0a 6c 69  6e 65 32 0a 6c 69 6e 65  |line1.line2.line|
00000010  33 0a 6c 69 6e 65 34 0a  6c 69 6e 65 00 35 0a 6c  |3.line4.line.5.l|
00000020  69 6e 65 36 0a 6c 69 6e  65 37 0a 6c 69 6e 65 38  |ine6.line7.line8|
00000030  0a 6c 69 6e 65 39 0a 6c  69 6e 65 31 30 0a        |.line9.line10.|
0000003e
me@here:~$ 

Is there a trick in Vim to jump to a byte position? Or as close as possible?

Ribeiro answered 14/8, 2013 at 17:50 Comment(2)
Not sure if this will work in your case, but have you tried converting the offset to decimal then using :goto 1234?Nineteen
You don't have to use hexadecimal numbering by the way, you can format the hd output any way you want. Take a look at the man page.Cahoon
C
71

Yes, there is. It's the normal mode command go.

From :h go:

[count]go         Go to {count} byte in the buffer.

For example, 42go jumps to byte 42.

In order to jump to a hexadecimal or octal address you would need to construct a command with :normal! go or the equivalent :goto, and the str2nr() or printf() function.

:exe 'normal! ' . str2nr('0x2a', 16) . 'go'
:exe 'goto' str2nr('0x2a', 16)
Cahoon answered 14/8, 2013 at 17:56 Comment(1)
Watch out though: Vim's byte positions are 1-based (like the column and line numbers), not zero-based.Forbore
C
6

While this question has merit of its own, may I suggest that you're doing this whole thing in a rather roundabout fashion, using all of hd, grep, Vim, and the shell?

Why not use Vim and only Vim for the whole process?

The key is at :h 23.4, "Binary files": Vim can be your hex editor.

After starting Vim with your file, vim -b myfile, the workflow becomes:

:%!xxd -g1<Enter>
/00 <Enter>
...
...
:%!xxd -r<Enter>

Be sure to read :h hex-editing, too, for more details.

Cahoon answered 14/8, 2013 at 20:38 Comment(0)
C
3

Yes, using :help starting.txt you can find the help about all vim command line arguments.

Using the above goto command given by several others you should be able to give vim a command from the command line to go to the byte offset such as

vim "+exe 'goto' str2nr('0x2a', 16)" file

There is a compile time flag described at :help +byte_offset you can check for using the :ve command. My Ubuntu version has it compiled in by default.

Cornhusk answered 14/8, 2013 at 18:33 Comment(2)
vim --version | grep byte_offset and look for a + or - is generally faster that scanning :versionClause
Although :echo has("byte_offset") is even faster. Just check to see that the output is a 1.Clause

© 2022 - 2024 — McMap. All rights reserved.