Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert regexp in vim

Tags:

regex

vim

There's a few "how do I invert a regexp" questions here on Stack Overflow, but I can't find one for vim (if it does exist, my Google-fu is lacking today).

In essence I want to match all non-printable characters and delete them. I could write a short script, or drop to a shell and use tr or something similar to delete, but a vim solution would be dandy :-)

Vim has the atom \p to match printable characters, however trying to do this :s/[^\p]//g to match the inverse failed and just left me with every 'p' in the file. I've seen the (?!xxx) sequence in other questions, and vim seems to not recognize this sequence. I've not found seen an atom for non-printable chars.

In the interim, I'm going to drop to external tools, but if anyone's got any tricks up their sleeve to do this, it'd be welcome :-)

Ta!

like image 421
Chris J Avatar asked Mar 24 '10 15:03

Chris J


2 Answers

Unfortunately you can't put \p in character classes, although that would be a nice feature. However you can use the negative-lookahead feature \@! to build your search:

/\p\@!.

This will first make sure that the . can only match when it is not a \p character.

like image 195
too much php Avatar answered Sep 16 '22 15:09

too much php


I'm also a little puzzled why you can't use the \p. But, [:print:] works fine:

:s/[^[:print:]]//g
like image 22
dsummersl Avatar answered Sep 19 '22 15:09

dsummersl