How to Remove Carriage Return in Vi

I have a file that's failing due to a single carriage return at the end (0x0D). It was originally a carriage return/new line combination (0x0D, 0x0A). I've tried:

  • deleting it in INSERT mode (deletes the last printing character and leaves the carriage return)
  • %s/\r// (E486: Pattern not found: \r)
  • %s/^M// (Ctrl+V, Ctrl+M to get ^M) (E486: Pattern not found: ^M)
  • J to join with the next line (appears to do nothing to the last line)

All of them leave the carriage return in place.

How can I get rid of this character from within vi (not vim)?

EDIT

Now it appears it's adding a new line character (0x0A) whenever I save the file. So, the 0x0D is gone, but I still have an invalid character at the end of my file.

I tried adding two blank lines to see if it would add 0x0A or 0x0D, 0x0A (I'm using PuTTY from Windows) and it added two 0x0A characters, for a total of three.

If you have three lines, shouldn't you only need two line separators?

  • %s/\n// (no error, but doesn't remove the character)
0

1 Answer

I finally found an answer elsewhere:

:set noendofline binary

Removes the final new line when the file is saved.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest