Find & Replace non-printable characters in vim

When I converted a file from EBCDIC format to ASCII format using an online tool, there are many non-printable characters in the ASCII file. So, this ASCII file is completely unreadable. So, I followed the below steps to make it readable.

1) Find hexa value of that non-printable character. Move your cursor to that character and press ‘ga’.

2) In escape mode, execute this ‘:%s/\%x85/\r/gc’. In my case, hexadecimal value of that non-printable character is 85. I replaced that with ‘\r’.

Some characters like ‘Ctrl+M’ can be printed by ‘Ctrl+v’ + ‘Ctrl+M’. But, in my case, 0x85 is that non-printable character and could not print it using ‘Ctrl+v’.



  1. Maximus said,

    December 20, 2007 at 12:08 pm

    I would like to see a continuation of the topic

  2. Jonathan Maes said,

    January 19, 2008 at 5:46 am

    This did not work for me, but if you switch it to Octal, which you get using the ga then it worked for me! However this tip was brilliant!! here is the octal search

    thank you so much!!

  3. mike shryne said,

    February 12, 2008 at 2:41 am

    the Hex worked fine and saved me a pile of time!

  4. Ari said,

    October 9, 2008 at 10:04 pm

    This was very helpful, thank you very much!

  5. July 7, 2009 at 7:42 pm

    A million thanks!

    I happened onto some text in an email that I pasted in VIM.

    Using your tip enabled me to get it cleaned up.

  6. Raja said,

    July 29, 2009 at 12:34 pm

    Darn. Time less how to man. I’ll blog about itas well. Thanks for the info

  7. Gregg said,

    September 5, 2009 at 5:24 am

    Thanks very kindly for the tip!!

    If your hex number is longer than 2 chars you have to use a different label than ‘x’ I found.

    From ‘:help /\%x’:

    \%d123 Matches the character specified with a decimal number. Must be
    followed by a non-digit.
    \%o40 Matches the character specified with an octal number up to 0377.
    Numbers below 040 must be followed by a non-octal digit or a non-digit.
    \%x2a Matches the character specified with up to two hexadecimal characters.
    \%u20AC Matches the character specified with up to four hexadecimal
    \%U1234abcd Matches the character specified with up to eight hexadecimal

    I love this tip though, it just didn’t quite work for me, then I found the ‘:help /\%x’ and thought I’d share.

    Thanks again!!

    • cynsa said,

      March 29, 2012 at 3:51 am

      oh I find this hopelessly confusing, I have to admit. ga reveals I’m trying to match <> 153, Hex 0099, Octal 231

      however, nothing I do will match this character, which when viewed by a browser is a TM char.

      :%s/\%x99//gc gives me:
      E71: Invalid character after \%

      So I researched an got that there was something wrong with my .magic visual settings. I added a \v and get:
      E486: Pattern not found: \v\%x99

      Just in case it’s the \v I do a regular search:
      and get an immediate match.

      Next I try to match the octal:
      E486: Pattern not found: \v\%o231

      I do a little research and I discover the following:
      “The character literal ™ you posted is not U+0099 (a control character), but U+2122 (TRADE MARK SIGN)….
      Decoding U+0099 in Windows does result in a trademark grapheme. I guess this is a bug.”

      But I don’t know what that means in terms of vi successfully matching it. All I know is I’m TEARING MY HAIR OUT.


      I try for octal and get:

  8. ronaldduncan said,

    October 31, 2009 at 7:37 pm

    Really liked this tip.

    We use a file format with %x1e (ASCII 30 Unit Separator) for record separators and %x1f (ASCII 31 Record Separator) for field separators, and this makes it quick to convert back and forward between CSV/Tab delimited and our format.

  9. May 7, 2010 at 4:55 pm

    […] Referência: Find & Replace non-printable characters in vim […]

  10. Akash said,

    June 25, 2010 at 3:00 am

    Great tip, thank you very much for posting, this saved me a boatload of time and headaches, can’t thank you enough!

  11. Pooh said,

    December 28, 2010 at 10:52 pm

    YES you are my new hero, I’ve been trying to do this for ages!

  12. Grant said,

    March 8, 2011 at 8:50 am

    No need to look up the hex number. Just move the cursor over the unprintable character in question and type vy to copy it into the register.

    Then when you go into ex mode to type in the search command, type followed by ” in order to paste the contents of that register.


    And when I say what I mean is hold control and type r.

  13. Joel Robinson said,

    March 26, 2011 at 4:00 am

    This tip was a huge help. I was trying to export an Xbase file to SQL. Some hex character kept showing up in the memo fields. Was able to clean everything up with this tip

  14. chakri said,

    August 27, 2011 at 1:01 am

    nice post

  15. Antonio said,

    November 8, 2011 at 6:34 pm

    You helped me a lot, I had ^M characters instead of regular line breaks! thx!

  16. maximus502 said,

    May 21, 2012 at 5:00 pm

    How can we make this dynamic ?? what I mean is what if I dont want to open the file and run “%s/\%x85/\r/g” i wan to run it under shell script. I tried doing this using below command:
    sed ‘s/\%x85/\r/g’ file1 > file2

    here file1 is like ANC

    But the above sed command is not working

    any ideas??

    Thanks In advance

  17. Mike said,

    September 25, 2012 at 10:33 pm

    Awesome… thanks.

  18. Adi Ö. said,

    January 24, 2014 at 7:46 pm

    Danke! Danke! Danke! ich suche seit Tagen nach einer Lösung und das hier ist es!

  19. Ian said,

    February 6, 2014 at 1:23 am

    if you’re using sed from the command line remove the %

    sed ‘s/\x85/\r/g’ file1 > file2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: