[linux] Convert DOS line endings to Linux line endings in Vim

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

This question is related to linux vim file editor dos2unix

The answer is


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).


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


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

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.


:%s/\r\+//g

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


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

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.


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

:%s/\r\+//g

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


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

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}";
}

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


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.


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


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.


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

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

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

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.


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


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

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.


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.


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.


: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.


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

From: File format

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


: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.


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

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.


: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.


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

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

(See: Convert between Unix and Windows text files)


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.


To run directly in a Linux console:

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

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.


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


From: File format

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


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}";
}

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

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 <[email protected]>

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

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

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

(See: Convert between Unix and Windows text files)


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


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


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

To run directly in a Linux console:

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

:%s/\r\+//g

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


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

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.


From: File format

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


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.


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

:%s/\r\+//g

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


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

To run directly in a Linux console:

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

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 :)


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


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


You can use:

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

Or the dos2unix utility.


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 :)


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


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


:%s/\r\+//g

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


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.


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


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 <[email protected]>

: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.


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

(See: Convert between Unix and Windows text files)


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


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

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.


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

From: File format

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


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

: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.


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

From: File format

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


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).


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

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.


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.


: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.


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.


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

:%s/\r\+//g

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


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

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


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.


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

From: File format

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


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.


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


:%s/\r\+//g

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


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

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


:%s/\r\+//g

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


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


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


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.


From: File format

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


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

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}";
}

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 <[email protected]>

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.


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


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

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.


You can use:

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

Or the dos2unix utility.


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

You can use:

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

Or the dos2unix utility.


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.


From: File format

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


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.


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}";
}

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

You can use:

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

Or the dos2unix utility.


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 :)


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

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.


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).


: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.


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.


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

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

(See: Convert between Unix and Windows text files)


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


To run directly in a Linux console:

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

: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.


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

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

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

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 :)


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to vim

Why does using from __future__ import print_function breaks Python2-style print? How to run vi on docker container? How can I install MacVim on OS X? Find and replace strings in vim on multiple lines Running Python code in Vim How do I set the default font size in Vim? Move cursor to end of file in vim Set encoding and fileencoding to utf-8 in Vim How to select all and copy in vim? Why I've got no crontab entry on OS X when using vim?

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to editor

Select all occurrences of selected word in VSCode Change the Theme in Jupyter Notebook? How to view Plugin Manager in Notepad++ Set language for syntax highlighting in Visual Studio Code Copy text from nano editor to shell How do I duplicate a line or selection within Visual Studio Code? How to set editor theme in IntelliJ Idea How to change background color in the Notepad++ text editor? What is the difference between Sublime text and Github's Atom What are the advantages of Sublime Text over Notepad++ and vice-versa?

Examples related to dos2unix

Convert line endings How can I run dos2unix on an entire directory? Convert DOS line endings to Linux line endings in Vim