Emacs search and replace of unprintable characters

Posted:

A common problem facing emacs users is to replace a certain sequence of characters in a buffer with either a newline or tab or something else equally awkward. The solution is relatively simple.

Invoke search and place as normal (M-%), enter the text to be replaced, enter the replacing character using the following chart:

  • Newline(\n): C-qC-j
  • Tab(\t): C-qC-i
  • Carriage return(\r): C-qC-m

See the pattern? C-q is an escape sequence and C-[char] is the position in the alphabet of the ASCII offset of the replacement character. OK, perhaps that’s a little tangled. But now you know.