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








December 3rd, 2007 at 1:38 pm
You can also run the linux program dos2unix on the file to achieve this
December 3rd, 2007 at 2:14 pm
Or dostou sure, but the point was not having to exit the file to run a second utility.
December 10th, 2007 at 9:28 pm
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