HOME BLOG

How to search and replace unprintable character with Vim

Posted on: October 14th, 2021 by Olu No Comments

Hi folks,

In this post I talk about how to search and replace unprintable character with Vim. I once found myself in a situation where some code wouldn’t run in my file due to an unknown character. It turned out the character wasn’t visible, so I had to find a way to remove it. If you find yourself in a similar situation here’s what to do:

First try to set encoding type and see if that works.

:set encoding=utf-8

If that doesn’t work, you can place the cursor on the unprintable character and press keys ga. This will show the decimal/hexadecimal/octadecimal code for the character. You can then replace it with command:

:%s/\%xYY/yournewchar/g

Or for multibyte character use:

:%s/\%uYYYY/yournewchar/g

See the following for details

:help character-class

 

Reference

How to search and replace an unprintable character. Stackoverflow. https://stackoverflow.com/questions/2798398/how-to-search-and-replace-an-unprintable-character/2801132

Leave a Reply