Removing DOS linebreaks from your files using Vim

This morning I’m debugging an issue on a script that gets cronned every minute. I’ve a shell open on the server and the file in question open using Vim and I notice each line has a trailing ^M…. Aargh the dreaded DOS linebreak.

Dusting off my rusty vim-foo I simply do a search and replace using Vim without having to exit the file.

The command is:

:%s/^M//g

Job done, well actually, no, there is still an issue with the script, but now I at least know that ^M is not the culprit.

Note: To get the ^M I did NOT type a caret and M, I typed Ctrl-V then Ctrl-M


Comments

3 responses to “Removing DOS linebreaks from your files using Vim”

  1. You can also run the linux program dos2unix on the file to achieve this

  2. Or dostou sure, but the point was not having to exit the file to run a second utility.

  3. Hi,

    I’d suggest you make sure you have a ~/.vimrc etc. When using VIM I /never/ see ^M characters (at worst it just says ‘dos’ in the status bar) – unless I’m dealing with a file that has both dos and Unix file endings (e.g. svn conflict file).

    To change the line endings of a file, you can use :set fileformat=unix (or =dos)

    David.
    (p.s. the openid box has the same name as the other field, so when posting this the first time, my browser had ‘kindly’ filled in the openid field with an invalid value, so I ended up loosing the comment 🙂 )

Leave a Reply

Your email address will not be published. Required fields are marked *