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

前端 未结 10 558
感动是毒
感动是毒 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:35

    Oops. That ^J^M needs to be entered as two literal characters. Use c-q c-j, c-q c-m and for the replacement string, use c-q c-j.

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

    No need for hexl-mode for this. Just do a global-search-and-replace of ^J^M with ^J Works for me. :) Then save the file, kill the buffer, and revisit the file so the window shows the new file mode (Unix vs DOS).

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

    From http://www.xsteve.at/prg/emacs/xsteve-functions.el:

    ;02.02.2000
    (defun xsteve-remove-control-M ()
      "Remove ^M at end of line in the whole buffer."
      (interactive)
      (save-match-data
        (save-excursion
          (let ((remove-count 0))
            (goto-char (point-min))
            (while (re-search-forward (concat (char-to-string 13) "$") (point-max) t)
              (setq remove-count (+ remove-count 1))
              (replace-match "" nil nil))
            (message (format "%d ^M removed from buffer." remove-count))))))
    

    Add this to your .emacs and run it via M-x xsteve-remove-control-M or bind it to a easier key. It will strip the ^Ms in anymode.

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

    I use this function:

    (defun l/cr-sanitise ()
      "Make sure current buffer uses unix-utf8 encoding.
    If necessary remove superfluous ^M. Buffer will need to be saved
    for changes to be permanent."
      (interactive)
        (set-buffer-file-coding-system 'utf-8-unix)
        (delete-trailing-whitespace)
        (message "Please save buffer to persist encoding changes."))
    
    0 讨论(0)
提交回复
热议问题