Converting the ^M characters back to newline on linux and mac

Sometimes when I saved a sheet from Excel on a Windows or Mac OSX computer as a tab-delimited file for further data processing, I frequently encountered the annoying ‘^M’ issues. To save my own time and to help the others who have the same issues, here are the solutions:

Open the file you want to convert with vim, then replace the ‘^M’ characters to \r like this:


Here CTRL-V+CTRL-M really means hold your Control-V and Control-M keys, which will be finally shown as ‘^M’ in place.

Alternatively, one can simply use the dos2unix command:

# install dos2unix
sudo apt install dos2unix   # for ubuntu 
brew install dos2unix       # for macOS

# convert input files
dos2unix input_file1 [input_file2 ... input_filen]        # for linux
dos2unix -c mac input_file1 [input_file2 ... input_filen] # for macOS

Leave a Reply