In emacs, how to strip CR (^M) and leave LF (^J) characters?

前端 未结 10 557
感动是毒
感动是毒 2021-01-31 03:01

I am trying to use hexl mode to manually remove some special chars from a text file and don\'t see how to delete anything in hexl mode.

What I really want is to remove c

相关标签:
10条回答
  • 2021-01-31 03:16

    No need to find replace. Just use.

    M-x delete-trailing-whitespace
    

    You can also set the file encoding through

    C-x RET f unix
    
    0 讨论(0)
  • 2021-01-31 03:20

    (in hexl mode) I'm not sure that you can delete characters. I've always converted them to spaces or some other character, switched to the regular text editor, and deleted them there.

    0 讨论(0)
  • 2021-01-31 03:24

    Assuming you want a DOS encoded file to be changed into UNIX encoding, use M-x set-buffer-file-coding-system (C-x RET f) to set the coding-system to "unix" and save the file.

    0 讨论(0)
  • 2021-01-31 03:26

    You don't need to use hexl-mode. Instead:

    • open file in a way that shows you those ^M's. See M-x find-file-literally /path/to/file above. In XEmacs you can also do C-u C-x C-f and select binary encoding.
    • select the string you want replace and copy it using M-w
    • do M-% (query replace) and paste what you want to copy using C-y
    • present Enter when prompted to what replace it with
    • possible press ! now to replace all occurrences

    The point is that even if you don't how to enter what you are trying to replace, you can always select/copy it.

    0 讨论(0)
  • 2021-01-31 03:27

    There's also a command-line tool called unix2dos/dos2unix that exists specifically to convert line endings.

    0 讨论(0)
  • 2021-01-31 03:28

    If you want to remove a carriage return (usually displayed as ^M) and leave the line feed. You can just visit the file w/out any conversion:

    M-x find-file-literally /path/to/file
    

    Because a file with carriage returns is generally displayed in DOS mode (hiding the carriage returns). The mode line will likely display (DOS) on the left side.

    Once you've done that, the ^M will show up and you can delete them like you would any character.

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