Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search and replace an unprintable character

Tags:

vim

I've a file that was exported from Word and it replaced all quotes with strange unicode characters which aren't correctly displayed in vim. So now I want those characters to be replaced with quotes, but I don't know how to enter this character in

:%s/???/'/g 

The characters look like this: ~U ~R. But of course I can't just mark them with mouse and paste in the command.

like image 970
Krzysztof Krasoń Avatar asked May 09 '10 16:05

Krzysztof Krasoń


People also ask

How do I find non-printable characters in a text file?

Option #1 - Show All Characters 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 do you remove non-ascii characters?

Use . replace() method to replace the Non-ASCII characters with the empty string.


2 Answers

You can try setting the encoding type and see if it fixes the visalizations of those characters:

:set encoding=utf-8 

then you can use them directly. Alternatively, you can place your cursor on the unprintable character and hit ga, it will show the decimal/hex/octal code of that character, then you can substitute it with:

:%s/\%xYY/substitute/g 

where YY is the hex code of the char, if it's multibyte:

:%s/\%uYYYY/substitute/g 

for details:

:help character-classes 

Note that you can search and match with \%xff or \%uabcd but will be unable to substitute with it.

like image 60
Matteo Riva Avatar answered Sep 24 '22 14:09

Matteo Riva


I usually:

  1. delete the character with: x
  2. undo my change with: u
  3. do the substitute thanks to c_CTRL-R: :%s/^R"/'/g
like image 44
Luc Hermitte Avatar answered Sep 21 '22 14:09

Luc Hermitte