sponsor Vim development Vim logo Vim Book Ad

basic Tip #565: never see ^M again

 tip karma   Rating 71/33, Viewed by 2679 

created:   September 26, 2003 17:40      complexity:   basic
author:   Usman Latif      as of Vim:   5.7

There have been plenty of tips dealing with ridding of ^M characters
appended to dos text files. However, all of the previous tips involve
some typing. With the following command in your vimrc, you won't have to
type anything. Moreover, you are not likely to see ^M characters again
at all, they get removed before you get to view the file. The only
exception being readonly files.

autocmd BufRead * silent! %s/^M$//

Note: Use <C-V><C-M> to enter ^M and not caret followed by M

 rate this tip  Life Changing Helpful Unfulfilling 

<<mouse wheel for scroll only - disable paste on middle button press. | Autocomplete with TAB when typing words >>

Additional Notes

zzapper@ntlworld.com, September 29, 2003 6:59
# \r easier to type tham ^m
# cope with possible multiple trailing ^m's

autocmd BufRead * silent! %s/\r\+$//

# also delete pesky trailing whitespace at the same time
autocmd BufRead * silent! %s/[\r \t]\+$//
natter_SIHT_EVOMER with aeiGARBAGE.ca, September 29, 2003 7:00
The fileformat (:help ff) variable should help you.  Usually, you see ^M when opening a DOS file in Unix format.  The file format should be set automatically to the right thing, but if that is not the case, type: ":set ff=dos".
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo