Removing carriage-return (^M) characters in Linux text files

You may notice that when you move text files from a windows system to linux you may encounter an issue where there is a end-of-line ^M (carriage-return) character. In DOS/Windows the end-of-line has a carriage return/newline combo, but in *nix end-of-line is denoted by a single newline. This can cause lots of errors/issues especially if you are launching a script. To get rid of this check out the one-liner below:


vim -T dumb --noplugin -n -es -c "set nomore" -c ":set fileformat=unix" -c ":wq" <FILE_NAME>

Parameter breakdown

  • -T dumb = avoids errors in case the terminal detection goes wrong.
  • --noplugin = do not load plugins.
  • -n = mo swapfile.
  • -es = ex mode + silent batch mode -s-ex (Must be given in that order).
  • -c 'set nomore' = suppress the more-prompt when the screen is filled with messages or output to avoid blocking.
  • -c ":set fileformat=unix" = gets rid of the ^M in the crlf by just changing the format.
  • -c ":wq" = write, quit.

.bashrc alias

You can also put this in as a bash alias. Syntax: nom <FILENAME>

alias nom='vim -T dumb --noplugin -n -es -c "set nomore" -c ":set fileformat=unix" -c ":wq"'

NOTE1: To read more about running vim on the command line check out this post.

Leave a Reply

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