Bert Hekman
Bert Hekman

Reputation: 9247

Convert DOS/Windows 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?

Upvotes: 839

Views: 722132

Answers (29)

pjz
pjz

Reputation: 43117

dos2unix is a commandline utility that will do this.

In Vim, :%s/^M//g will if you use Ctrl-v Ctrl-m to input the ^M (On Windows, use Ctrl-q Ctrl-m instead).

Vim may not show the ^M characters if the file is loaded with a dos (Windows) file format. In this case you can :set ff=unix and then save the file with :w or :w! and Vim will reformat the file as it saves 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.

Upvotes: 1229

Vittore Marcas
Vittore Marcas

Reputation: 1215

To delete these DOS/Windows line endings characters all at once, regardless of where they occur in a line (this is not a good idea if two lines were separated only by a CR because the command joins the lines together):

:%s/\r//g

Reference: File format -> 6.Removing unwanted CR or LF characters.

But, You could choose to convert these DOS/Windows line endings into Unix.

That means, to convert the current file from any mixture of CRLF/LF-only line endings, so all lines end with LF only:

:update              Save any changes. 
:e ++ff=dos          Edit file again, using dos file format ('fileformats' is ignored).
:setlocal ff=unix    This buffer will use LF-only line endings when written.
:w                   Write buffer using unix (LF-only) line endings. 

Reference: File format -> 3.Converting the current file.

In the above, replacing :set ff=unix with :set ff=mac would write the file with mac (CR-only) line endings. Or, if it was a mac file to start with, you would use :e ++ff=mac to read the file correctly, so you could convert the line endings to unix or dos.

More reference:

1.Configure line separators -> Change line separators for the current file(IntelliJ IDEA).

2.Configure line separators -> Configure line separators for new files(IntelliJ IDEA).

3.Vim Tips Wiki -> File format.

Upvotes: 1

chipfall
chipfall

Reputation: 360

This is a little more than you asked for but:

nmap <C-d> :call range(line('w0'),line('w$'))->map({_,v-> getline(v)})->map({_,v->trim(v,join(map(range(1,0x1F)+[0xa0],{n->n->nr2char()}),''),2)})->map({k,v->setline(k+1,v)})<CR>

Run this and :set ff=unix|dos and no more need for unix2dos.

  • the single arg form of trim() has the same default mask above, plus 0X20 (an actual space) instead of 0x1F
  • that default mask clears out all non-printing chars including non-breaking spaces [0xa0] that are hard to find
  • create a list of lines from the range of lines
  • map that list to the trim function with using the same mask code as the source, less spaces
  • map that again to setline to replace the lines.
  • all :set fileformat= does at this point is choose which eol to save it with, dos or unix
  • it should be pretty easy to change the range of characters above if you want to eliminate or add some

Upvotes: 0

Victor S.
Victor S.

Reputation: 2777

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.

Upvotes: 23

CBuzatu
CBuzatu

Reputation: 785

To run directly in a Linux console:

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

Upvotes: 12

Tallak Tveide
Tallak Tveide

Reputation: 369

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.

Upvotes: 2

Albaro Pereyra
Albaro Pereyra

Reputation: 49

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]>

Upvotes: 0

Santle Camilus
Santle Camilus

Reputation: 985

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.

Upvotes: -1

Christy Wald
Christy Wald

Reputation: 329

You can use:

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

Or the dos2unix utility.

Upvotes: 2

Thomas PABST
Thomas PABST

Reputation: 1

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

Upvotes: 0

Stefan Sj&#246;berg
Stefan Sj&#246;berg

Reputation: 183

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.

Upvotes: 6

loadaverage
loadaverage

Reputation: 1035

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.

Upvotes: 0

user3220487
user3220487

Reputation:

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

Upvotes: 1

Sparkida
Sparkida

Reputation: 422

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

Upvotes: 9

Michael Douma
Michael Douma

Reputation: 1194

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

(See: Convert between Unix and Windows text files)

Upvotes: 10

ajitomatix
ajitomatix

Reputation: 388

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

Upvotes: 7

Venkataramesh Kommoju
Venkataramesh Kommoju

Reputation: 1099

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

Upvotes: 8

dsm
dsm

Reputation: 10395

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

Upvotes: -1

t-dub
t-dub

Reputation:

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

Upvotes: 181

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

Upvotes: 23

Sylvain Defresne
Sylvain Defresne

Reputation: 44593

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

Upvotes: 147

Alex Gartrell
Alex Gartrell

Reputation: 2554

From: File format

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

Upvotes: 15

David Webb
David Webb

Reputation: 193804

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.

Upvotes: 5

Rob Wells
Rob Wells

Reputation: 37159

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

Upvotes: 4

mercutio
mercutio

Reputation: 22597

:%s/\r\+//g

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

Upvotes: 20

Ludvig A. Norin
Ludvig A. Norin

Reputation: 5333

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

Upvotes: 342

Dannid
Dannid

Reputation: 1697

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

Upvotes: 5

JayG
JayG

Reputation: 4459

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

Upvotes: 2

user3379574
user3379574

Reputation: 1

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

Upvotes: 0

Related Questions