How do I search for, delete, or replace unprintable (eight-bit or control) characters?
To search for a single character that appears in the buffer as, for example, ‘\237’, you can type C-s C-q 2 3 7. Searching for all unprintable characters is best done with a regular expression (regexp) search. The easiest regexp to use for the unprintable chars is the complement of the regexp for the printable chars.
- Regexp for the printable chars: ‘
[\t\n\r\f -~]’ - Regexp for the unprintable chars: ‘
[^\t\n\r\f -~]’
To type these special characters in an interactive argument to isearch-forward-regexp or re-search-forward, you need to use C-q. (‘\t’, ‘\n’, ‘\r’, and ‘\f’ stand respectively for TAB, LFD, RET, and C-l.) So, to search for unprintable characters using re-search-forward:
M-x re-search-forward RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET
Using isearch-forward-regexp:
C-M-s [^ TAB LFD C-q RET C-q C-l SPC -~]
To delete all unprintable characters, simply use replace-regexp:
M-x replace-regexp RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET RET
Replacing is similar to the above. To replace all unprintable characters with a colon, use:
M-x replace-regexp RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET : RET