DOS end of lines in Vim

Sometimes I get annoying ^M chars when opening files using Vim – obviously it’s a DOS EOL which mixes the picture.

The good place to seek for fixes and workarounds is Vim Wikia, which not only shows you what to do, but also gives you a brief background on what is going on (if you are unaware).

I am generally happy with dos2unix utility to convert files, but sometimes it seems to be unable to fix the issue. In such situations I do a simple search/replace, right inside of Vim (in ex mode):

:%s/\r/\r/g

In my opinion it is real magic – it replaces carriage returns (CR) with line-feeds (LF), but the actual command reads: do a replacement of \r with \r, right?
Well, Vim is clever enough to look into file-type, and if it’s unix – it presumes that first \r can be either CR or LF, while second (replacement) is always LF :)

If it doesn’t work for you check your buffer’s file type:

:set ff?

I only recently moved to Vim, but start loving it for such small but extremely useful gems.

Should you have any questions/suggestions – hit me back on my tweeter.

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit
  • Technorati
  • email
  • Print
  • DZone
  • eKudos
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter
,
Trackback

2 comments untill now

  1. I usually do a %s///g to eliminate thowse things :p

  2. oups I mean %s/[ctrl+v][ctrl+M]//g

Add your comment now