41

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

What I really want is to remove carriage return and keep linefeed characters.
Is Hexl mode the right way to do this?

4
  • 1
    If it's a text file... why use hexl mode? Jul 23, 2009 at 15:02
  • I'm trying to remove a carriage return and leave a line feed...don't ask - the program that's reading this config file wants it that way. Jul 23, 2009 at 15:32
  • 1
    +1 I had a similar issue. For some odd reason, a bunch of hex characters were prepended to the beginning of some of my text files. I just want to delete those characters, but can't see them in regular buffer mode. Why is the "delete" feature so hard for hexl-mode? It must be there.
    – User1
    Jun 22, 2010 at 14:14
  • Closely related: stackoverflow.com/q/1822849.
    – Henke
    Oct 14, 2022 at 13:32

10 Answers 10

101

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
33

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.

2
  • I can't believe how few votes this has! This would totally save a novice (or forgetful) Emacs user! Jan 13, 2015 at 14:01
  • 2
    Yes, this should be higher. Also, you can just replace ^M and ignore the ^J. Nov 13, 2015 at 17:29
10

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).

3
  • 2
    Why even bother to do this? Do what keysersoze suggests and use dos2unix and/or unix2dos. Jul 27, 2009 at 19:20
  • Instead of killing the buffer and revisiting the file, you can click on the EOL type indicator in the mode line to cycle through the different options. This way you won't lose your kill ring.
    – phils
    Dec 12, 2010 at 0:39
  • 1
    would have been helpful to note how to actually do this - M-% doesn't work like this for me
    – baxx
    Oct 18, 2015 at 21:20
9

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

8

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.

6

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.

4

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.

1
  • Thanks, didn't know about find-file-literally, actually a better tool for seeing control characters in text files than hexl mode which I'd been using. Nov 22, 2011 at 15:52
3

(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
2

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."))
1

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.

1
  • 1
    This is a great solution. One command to fix the whole file instantly.
    – DOKKA
    Aug 20, 2014 at 17:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.