Use vim to search by hex code

SearchVimHex

Search Problem Overview


I need to clean up a file. We have an xml parser that runs on it that is failing due to an escape character (0x1B) in the file. How do I use vim to find where in the file that character is so I can remove it?

Example file:

<?php
echo "Hello, world.\n";                           
?>

After conversion:

0000000: 0a3c 3f70 6870 0a65 6368 6f20 2248 656c  .<?php.echo "Hel
0000010: 6c6f 2c20 776f 726c 642e 5c6e 223b 0a3f  lo, world.\n";.?
0000020: 3e0a  

So I delete a char: (in this example, the 'H')

0000000: 0a3c 3f70 6870 0a65 6368 6f20 22 656c  .<?php.echo "Hel
0000010: 6c6f 2c20 776f 726c 642e 5c6e 223b 0a3f  lo, world.\n";.?
0000020: 3e0a

Notice how the first line isn't wide enough anymore. When I convert it back, I get:

^@<?php
echo "el^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@> 

Search Solutions


Solution 1 - Search

Search (e.g. using /) for \%x1b.

You can also type control characters, including escape, into the command line by preceding them with Ctrl-V. So type /, Ctrl-V, Esc, Enter.

Solution 2 - Search

Transform vim into a hex editor by doing [escape] :%!xxd. Then search for 0x1B (/1B).

Solution 3 - Search

You can discover what a hex value of a character by putting the cursor on it and pressing ga

Below you will see a hex value of the character. Example for a <╬> character

> <╬> <|N> 206, Hex ce, Oct 316, Digr I>

Delete it by :

%s/\%xce//g 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid OneillView Question on Stackoverflow
Solution 1 - SearchNefrubyrView Answer on Stackoverflow
Solution 2 - SearchAlexander GesslerView Answer on Stackoverflow
Solution 3 - SearchDani KonoplyaView Answer on Stackoverflow