Convert DOS line endings to Linux line endings in Vim

LinuxVimFileEditorDos2unix

Linux Problem Overview


If I open files I created in Windows, the lines all end with ^M. How do I delete these characters all at once?

Linux Solutions


Solution 1 - Linux

dos2unix is a commandline utility that will do this, or :%s/^M//g will if you use Ctrl-v Ctrl-m to input the ^M, or you can :set ff=unix and Vim will do it for you.

There is documentation on the fileformat setting, and the Vim wiki has a comprehensive page on line ending conversions.

Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do :set ff=dos, so Vim will know it's a DOS file and use DOS conventions for line endings.

Solution 2 - Linux

Change the line endings in the view:

:e ++ff=dos
:e ++ff=mac
:e ++ff=unix

This can also be used as saving operation (:w alone will not save using the line endings you see on screen):

:w ++ff=dos
:w ++ff=mac
:w ++ff=unix

And you can use it from the command-line:

for file in *.cpp
do 
    vi +':w ++ff=unix' +':q' "$file"
done

Solution 3 - Linux

I typically use

:%s/\r/\r/g

which seems a little odd, but works because of the way that Vim matches linefeeds. I also find it easier to remember :)

Solution 4 - Linux

I prefer to use the following command:

:set fileformat=unix

You can also use mac or dos to respectively convert your file to Mac or MS-DOS/Windows file convention. And it does nothing if the file is already in the correct format.

For more information, see the Vim help:

:help fileformat

Solution 5 - Linux

:set fileformat=unix to convert from DOS to Unix.

Solution 6 - Linux

:%s/\r\+//g

In Vim, that strips all carriage returns, and leaves only newlines.

Solution 7 - Linux

From: File format

[Esc] :%s/\r$//

Solution 8 - Linux

In VIM:

:e ++ff=dos | set ff=unix | w!

In shell with VIM:

vim some_file.txt +'e ++ff=dos | set ff=unix | wq!'

e ++ff=dos - force open file in dos format.

set ff=unix - convert file to unix format.

Solution 9 - Linux

dos2unix can directly modify the file contents.

You can directly use it on the file, without any need for temporary file redirection.

dos2unix input.txt input.txt

The above uses the assumed US keyboard. Use the -437 option to use the UK keyboard.

dos2unix -437 input.txt input.txt

Solution 10 - Linux

Convert directory of files from DOS to Unix

Using command line and sed, find all files in current directory with the extension ".ext" and remove all "^M"

@ https://gist.github.com/sparkida/7773170

find $(pwd) -type f -name "*.ext" | while read file; do sed -e 's/^M//g' -i "$file"; done;

Also, as mentioned in a previous answer, ^M = Ctrl+V + Ctrl+M (don't just type the caret "^" symbol and M).

Solution 11 - Linux

tr -d '\15\32' < winfile.txt > unixfile.txt

(See: Convert between Unix and Windows text files)

Solution 12 - Linux

The following steps can convert the file format for DOS to Unix:

:e ++ff=dos     Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix     This buffer will use LF-only line endings when written.[A 2]
:w     Write buffer using Unix (LF-only) line endings.

Reference: File format

Solution 13 - Linux

To run directly in a Linux console:

vim file.txt +"set ff=unix" +wq

Solution 14 - Linux

With the following command:

:%s/^M$//g

To get the ^M to appear, type CtrlV and then CtrlM. CtrlV tells Vim to take the next character entered literally.

Solution 15 - Linux

The comment about getting the ^M to appear is what worked for me. Merely typing "^M" in my vi got nothing (not found). The CTRL+V CTRL+M sequence did it perfectly though.

My working substitution command was

:%s/Ctrl-V Ctrl-M/\r/g

and it looked like this on my screen:

:%s/^M/\r/g

Solution 16 - Linux

:g/Ctrl-v Ctrl-m/s///

CtrlM is the character \r, or carriage return, which DOS line endings add. CtrlV tells Vim to insert a literal CtrlM character at the command line.

Taken as a whole, this command replaces all \r with nothing, removing them from the ends of lines.

Solution 17 - Linux

I found a very easy way: Open the file with nano: nano file.txt

Press Ctrl + O to save, but before pressing Enter, press: Alt+D to toggle between DOS and Unix/Linux line-endings, or: Alt+M to toggle between Mac and Unix/Linux line-endings, and then press Enter to save and Ctrl+X to quit.

Solution 18 - Linux

You can use:

vim somefile.txt +"%s/\r/\r/g" +wq

Or the dos2unix utility.

Solution 19 - Linux

You can use the following command:
:%s/^V^M//g
where the '^' means use CTRL key.

Solution 20 - Linux

The below command is used for reformating all .sh file in the current directory. I tested it on my Fedora OS.

for file in *.sh; do awk '{ sub("\r$", ""); print }' $file >luxubutmp; cp -f luxubutmp $file; rm -f luxubutmp ;done

Solution 21 - Linux

In Vim, type:

:w !dos2unix %

This will pipe the contents of your current buffer to the dos2unix command and write the results over the current contents. Vim will ask to reload the file after.

Solution 22 - Linux

Usually there is a dos2unix command you can use for this. Just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.

BSD version:

dos2unix $FILENAME $FILENAME_OUT
mv $FILENAME_OUT $FILENAME

GNU version:

dos2unix $FILENAME

Alternatively, you can create your own dos2unix with any of the proposed answers here, for example:

function dos2unix(){
    [ "${!}" ] && [ -f "{$1}" ] || return 1;

    { echo ':set ff=unix';
      echo ':wq';
    } | vim "${1}";
}

Solution 23 - Linux

From Wikia:

%s/\r\+$//g

That will find all carriage return signs (one and more reps) up to the end of line and delete, so just \n will stay at EOL.

Solution 24 - Linux

This is my way. I opened a file in DOS EOL and when I save the file, that will automatically convert to Unix EOL:

autocmd BufWrite * :set ff=unix

Solution 25 - Linux

I wanted newlines in place of the ^M's. Perl to the rescue:

perl -pi.bak -e 's/\x0d/\n/g' excel_created.txt

Or to write to stdout:

perl -p -e 's/\x0d/\n/g' < excel_created.txt

Solution 26 - Linux

If you create a file in Notepad or Notepad++ in Windows, bring it to Linux, and open it by Vim, you will see ^M at the end of each line. To remove this,

At your Linux terminal, type

dos2unix filename.ext

This will do the required magic.

Solution 27 - Linux

I knew I'd seen this somewhere. Here is the FreeBSD login tip:

Do you need to remove all those ^M characters from a DOS file? Try

tr -d \\r < dosfile > newfile
    -- Originally by Dru <genesis@istar.ca>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBert HekmanView Question on Stackoverflow
Solution 1 - LinuxpjzView Answer on Stackoverflow
Solution 2 - LinuxLudvig A. NorinView Answer on Stackoverflow
Solution 3 - Linuxt-dubView Answer on Stackoverflow
Solution 4 - LinuxSylvain DefresneView Answer on Stackoverflow
Solution 5 - LinuxMartin Sj&#246;grenView Answer on Stackoverflow
Solution 6 - LinuxmercutioView Answer on Stackoverflow
Solution 7 - LinuxAlex GartrellView Answer on Stackoverflow
Solution 8 - LinuxVictor S.View Answer on Stackoverflow
Solution 9 - LinuxVenkataramesh KommojuView Answer on Stackoverflow
Solution 10 - LinuxSparkidaView Answer on Stackoverflow
Solution 11 - LinuxMichael DoumaView Answer on Stackoverflow
Solution 12 - LinuxajitomatixView Answer on Stackoverflow
Solution 13 - LinuxCBuzatuView Answer on Stackoverflow
Solution 14 - LinuxDave WebbView Answer on Stackoverflow
Solution 15 - LinuxDannidView Answer on Stackoverflow
Solution 16 - LinuxRob WellsView Answer on Stackoverflow
Solution 17 - LinuxStefan SjöbergView Answer on Stackoverflow
Solution 18 - LinuxChristy WaldView Answer on Stackoverflow
Solution 19 - LinuxJayGView Answer on Stackoverflow
Solution 20 - Linuxuser3220487View Answer on Stackoverflow
Solution 21 - LinuxTallak TveideView Answer on Stackoverflow
Solution 22 - LinuxdsmView Answer on Stackoverflow
Solution 23 - LinuxloadaverageView Answer on Stackoverflow
Solution 24 - LinuxThomas PABSTView Answer on Stackoverflow
Solution 25 - Linuxuser3379574View Answer on Stackoverflow
Solution 26 - LinuxSantle CamilusView Answer on Stackoverflow
Solution 27 - LinuxAlbaro PereyraView Answer on Stackoverflow