Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 351
Charles Ma Avatar asked Oct 02 '10 03:10

Charles Ma


People also ask

How do I find a non-printable character?

You can download Notepad++ and open the file there. Then, go to the menu and select View->Show Symbol->Show All Characters . All characters will become visible, but you will have to scroll through the whole file to see which character needs to be removed.

How can we view non-printable characters in a file in Unix?

[3] On BSD, pipe the ls -q output through cat -v or od -c ( 25.7 ) to see what the non-printing characters are. This shows that the non-printing characters have octal values 13 and 14, respectively. If you look up these values in an ASCII table ( 51.3 ) , you will see that they correspond to CTRL-k and CTRL-l.

How do I use special characters in Vim?

While in insert mode, you can insert special characters in Vim by pressing <ctrl-k> followed by a two-character lookup code. For example, <ctrl-k> ? 2 will insert the approximately equal symbol: ≈ .

How do I delete all non-printable characters?

Step 1: Click on any cell (D3). Enter Formula =CLEAN(C3). Step 2: Click ENTER. It removes non-printable characters.


2 Answers

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

like image 117
lincz Avatar answered Sep 30 '22 06:09

lincz


Say you want to replace ^C with C:

:%s/CtrlVC/C/g

Where CtrlVC means type V then C while holding Ctrl pressed.

CtrlV lets you enter control characters.

like image 25
ars Avatar answered Sep 30 '22 07:09

ars