1

The file I'm working on contains a hexdump. I pulled it directly out of memory, so it starts from an arbitrary offset, like this:

0x1234: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1244: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1254: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1264: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I want to "rebase" this hexdump so it looks like this:

0x0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

This means I need to subtract the value 0x1234 from each of those values. My current workflow looks like this:

  1. Fire up a python interpreter and convert 0x1234 to base-10 (it's 4660)
  2. Position the cursor over the first 0x1234 in my file
  3. Input 4660Ctrl-x
  4. Repeatedly input j. until I've hit all the lines

That first step is really annoying - since I'm working with hexadecimal numbers, it feels like I ought to be able to subtract a hexadecimal value directly, rather than converting via base-10.

Is there an easier way to do this?

1 Answer 1

1

I would do:

:execute "normal! Ctrl-r=Ox1234Enter\<C-x>"

The r= use the expression register. It let you enter an expression that Vim will convert and paste.

To repeat it you can use the same technique: j. but you could also record a macro and run it 1000 times:

Record the macro in register a:

qaj.a

Run it 1000 times:

1000@a

1
  • 2
    Also :global/^0x/normal! <C-r>=0x1234<Enter><C-v><C-x> (all <> keystrokes as actual keypresses)
    – D. Ben Knoble
    Dec 8, 2022 at 17:29

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.