Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to byte address in vim?

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?

like image 514
Alois Mahdal Avatar asked Aug 14 '13 17:08

Alois Mahdal


People also ask

How do I go to a specific character in Vim?

:goto 21490 will take you to the 21490th byte in the buffer. Show activity on this post. From the command line will open the file and take you to position 21490 in the buffer.

How do you jump in front of a line in Vim?

Press 0 to go to the beginning of a line, or ^ to go to the first non-blank character in a line.

How do I go to a specific column in vi editor?

You can use the cursor function. For example, to jump to column 25 of line 15, you can use :call cursor(15,25) .


1 Answers

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) 
like image 107
glts Avatar answered Sep 29 '22 04:09

glts