How do I replace or find non-printable characters in vim regex?

Charles Ma picture Charles Ma · Oct 2, 2010 · Viewed 47k times · Source

I have a file with some non-printable characters that come up as ^C or ^B, I want to find and replace those characters, how do I go about doing that?

Answer

lincz picture lincz · Apr 21, 2013

Removing control symbols only:

:%s/[[:cntrl:]]//g

Removing non-printable characters (note that in versions prior to ~8.1.1 this removes non-ASCII characters also):

:%s/[^[:print:]]//g

The difference between them could be seen if you have some non-printable-non-control characters, e.g. zero-width space:

enter image description here