tags:

views:

642

answers:

2

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^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@> 
+4  A: 

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

Alexander Gessler
Once I've opened it in that mode, how do I remove that char and save the file?
David Oneill
Alexander Gessler
I am not replacing the character, I am deleting it. Once it has been delete, the rows don't match up. Does VIM handle a shorter row when translating from the visual hex back into ascii?
David Oneill
Ups, sorry- you need to do switch back to ASCII using ´:%!xxd -r´ before you save. That should do the job, hopefully.
Alexander Gessler
That yeilds a broken file (presumably due to the mis-aligned lines) I'm going to edit my answer to show what I mean
David Oneill
+3  A: 

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>.

Nefrubyr
I'm not sure how much more detail is needed. Don't bother switching to hex representation. Vim will show an escape character as `^[`. Search for it using either of my methods and, with the cursor on it, delete it using `x`.
Nefrubyr
For posterity: nerfrubyr's comment was in regards on a comment I deleted when asking for clarification. It turns out I was typing something wrong. Both of these ideas work great for this.
David Oneill