How to find and remove the invisible characters in text file using emacs

前端 未结 3 1923
后悔当初
后悔当初 2020-12-30 07:10

I have a txt file named COPYING which is edited on windows. It contains windows style eol

$ file COPYING 
COPYING: ASCII English text, with CRLF line ter         


        
相关标签:
3条回答
  • 2020-12-30 07:51

    To see invisible characters, you can try whitespace-mode. Spaces and tabs will be displayed with a symbol in a different face. If the coding system is automatically being detected as dos (showing (DOS) on the status bar), carriage returns at the end of a line will be hidden as well. Run revert-buffer-with-coding-system to switch it to Unix or binary (e.g. C-x RET r unix) and they'll always show up as ^M. The binary coding system will display any non-ASCII characters as control characters as well.

    0 讨论(0)
  • 2020-12-30 07:54

    Emacs won't hide any character by default. Press Ctrl+Meta+%, or Esc then Ctrl+% if the former is too hard on your fingers, or M-x replace-regexp RET if you prefer. Then, for the regular expression, enter

    [^@-^H^K-^_^?]
    

    However, where I wrote ^H, type Ctrl+Q then Ctrl+H, to enter a “control-H” character literally, and similarly for the others. You can press Ctrl+Q then Ctrl+Space for ^@, and usually Ctrl+Q then Backspace for ^?. Replace all occurrences of this regular expression by the empty string.

    Since you have the file open in Emacs, you can change its line endings while you're at it. Press C-x RET f (Ctrl+X Return F) and enter us-ascii-unix as the new desired encoding for the file.

    0 讨论(0)
  • 2020-12-30 07:58

    Check out M-x set-buffer-file-coding-system. From the documentation:

    (set-buffer-file-coding-system CODING-SYSTEM &optional FORCE NOMODIFY)

    Set the file coding-system of the current buffer to CODING-SYSTEM. This means that when you save the buffer, it will be converted according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM, use M-x list-coding-systems.

    So, going from DOS to UNIX, M-x set-buffer-file-coding-system unix.

    0 讨论(0)
提交回复
热议问题