If I open files I created in Windows, the lines all end with ^M
.
How do I delete these characters all at once?
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/15482.webp)
- 8,307
- 3
- 19
- 14
-
1If you do a `hexdump -C badfile` and see `0x0d 0x0a` `"\r\n"` that is your problem. – Yzmir Ramirez Jun 08 '19 at 04:49
27 Answers
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.
![](../../users/profiles/8002.webp)
- 38,171
- 5
- 45
- 60
-
That was it for me thank you pal :> Bash wouldn't launch the script because of line endings. Stupid notepad really... – Gepsens Aug 09 '11 at 01:29
-
59`:%s/^M//g` should be `:%s/\r//g`, because `^M` just means "match capital "M" at the beginning of the line". – Bunyk Sep 12 '13 at 08:52
-
87
-
3**crtl-v** is no good, on windows it pastes clipboard contents to the command line. Solution `:%s/\r//g` worked for me, cheers @Bunyk – roblogic May 26 '15 at 03:32
-
I got confused when I tried this, because it seemed to take ages to do the conversion, when in fact it was just waiting for user input. – mwfearnley Jun 29 '16 at 15:45
-
4@ropata What you want on Windows is [**ctrl-q**](http://stackoverflow.com/a/426910/1028230). – ruffin Aug 31 '16 at 16:28
-
1dos2unix might be the better solution but only if it's present on the system. I don't think we should lead the answer with it. `set ff=unix` seems the best answer. – mwfearnley Feb 28 '17 at 17:21
-
3I must be missing something, because `set ff=unix` does nothing. Maybe it converts the file, but all of the `^M` characters are still there. – felwithe Mar 30 '17 at 16:11
-
1If you want to ensure you keep the new lines its best to actually do this :%s/\r/\r/g unix/vim will put in the right newline char with the replace \r – bjm88 Sep 29 '17 at 19:19
-
1both `^M` (Ctrl+V Ctrl+M) and `\r` are not found in my file but I definitely see them in diff (VIM - Vi IMproved 7.4) – vladkras Dec 12 '17 at 11:42
-
1`:%s/\r//g` or `:%s/^M//g` does not work. `:set ff=unix ` is work as desired. But It formats whole file, Not only those lines which contain ^m – Arun Kumar Sep 04 '19 at 11:21
-
There is good [source](https://kb.iu.edu/d/acux) for change or convert to and from Windows to UNIX. – Arun Kumar Sep 04 '19 at 11:34
-
1@felwithe - :set ff=unix then you :wq the file to write out the new format. I just did this two minutes ago to clean a file I got from a developer. – Andrew Beals Dec 27 '19 at 16:53
-
Use the vim set command to show the file format `:set ff?`. The command returns fileformat=
to indect current file format. – NKSM Apr 07 '20 at 21:16
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
![](../../users/profiles/1848654.webp)
- 79,257
- 6
- 70
- 127
![](../../users/profiles/16909.webp)
- 4,685
- 7
- 27
- 34
-
4Thank you very much. I tried vi +':wq ++ff=unix'
for a lazy one like me. – a_secenthusiast May 22 '16 at 07:24 -
3This should be the most upvoted answer. `:w +ff=unix` is so much nicer than most of the other stuff written here, and the bash script is a nice bonus. – mkasberg Jun 18 '17 at 03:42
-
1The `:e` commands don't appear to do anything on my Windows vim install. My view still shows the `^M` codes. – Grant Birchmeier Jun 20 '17 at 21:28
-
I read the `:e ++ff=unix` everywhere but the simplest for me that worked is `:w ++ff=unix` then quit & reopen file. – Olivier Pons Dec 06 '20 at 10:06
-
@OlivierPons but :e does the same thing as reopening the file? How is adding more unnecessary steps to your solution making it simpler? – Victor Zamanian Jan 08 '21 at 07:50
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 :)
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
-
9
-
4I've never had any problem with `:set ff=unix` before, but the file I opened today must have been particularly weird. Vim said it was already `fileformat=unix` but all the line endings were `^M`. This solution worked for me. – Chris B Mar 21 '13 at 08:55
-
16This solution adds unwanted extra lines for me, doubling the number of lines in the file. `:%s/\r//g` instead works for me. – Victor Zamanian Aug 22 '13 at 23:57
-
3Victor, your files likely have \r\n endings. the \r isn't read as a newline but the \n is. In the files I'm running into are \r and you have to add a newline character. – Joeyjoejoejr Feb 12 '15 at 20:59
-
astyle, dos2unix, vim fileformat, notepad, ultra-edit, etc. all didn't work; this did! Previous to trying this I had to open the file in Wordpad, paste into notepad and then paste into ultraedit before I could go back to VIM. Someone put varying types of linefeed types and EOF in files which then made it hard to find where function calls were in sources. grep returned junk for the file with mixed linefeed types and EOF. Disappointed that other methods didn't work. Thanks for a simple solution! – TheHairyOne Sep 19 '16 at 21:14
-
I tried the other solutions here, but this is the only one that worked for me. I had imported a CSV file into Excel (Mac OS X), and after transforming it a bit, exported it as a CSV. It ended up being a one-line file with `^M` separating what were the original lines. – user766353 Oct 12 '16 at 00:28
-
I’m not vim-ish enough to understand why, but this solution works for me in circumstances where the accepted answer failed – Chris S Nov 07 '16 at 15:06
-
When you do a `hexdump -C badfile` you can see the hex sequence `0x0d 0x0a` which is your problem. – Yzmir Ramirez Jun 08 '19 at 04:48
-
3@VictorZamanian's `:%s/\r//g` is the *only* general-purpose solution – especially for mixed-mode files containing a heterogeneous admixture of both DOS- and UNIX-style newlines. The canonical solutions (e.g., `:set ff=unix`, `:e ++ff=unix`) assume every line of the current buffer ends in the same newline style. Sometimes they do; sometimes they don't. *Cue sadface.* – Cecil Curry Sep 28 '19 at 03:24
-
@VictorZamanian's, [this](https://stackoverflow.com/a/65125795/7676971) method should help solve the problem with adding new lines. – Victor S. Jan 08 '21 at 07:00
-
@VictorS. ? I didn't have a problem adding newlines. I got extra newlines I *didn't* want. And if you read my comment to the end you'll see I had a solution already. Thanks though. – Victor Zamanian Jan 08 '21 at 07:48
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
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/5353.webp)
- 38,469
- 11
- 70
- 82
-
2This command doesn't appear to do anything on my Windows vim. My view still has `^M` chars in it. – Grant Birchmeier Jun 20 '17 at 21:29
-
:set fileformat=unix
to convert from DOS to Unix.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
-
2This actually resolved the issue for me. I wasn't able to find those characters while searching. – Captain Insaneo Aug 05 '15 at 14:10
:%s/\r\+//g
In Vim, that strips all carriage returns, and leaves only newlines.
![](../../users/profiles/1833960.webp)
- 3,923
- 4
- 26
- 54
![](../../users/profiles/1951.webp)
- 20,943
- 10
- 33
- 37
-
3For some reason above didn't work for me under windows gvim. But when changed to `:%s/\r//g` it worked like a charm. – soltysh Jan 23 '14 at 09:35
-
2
From: File format
[Esc] :%s/\r$//
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/10307.webp)
- 2,354
- 5
- 20
- 22
-
+1 for pointing to the official doc site. For anyone using the above link, see the section "Converting the current file" on that page. – the Tin Man Jan 07 '13 at 16:48
-
-
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).
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/1934975.webp)
- 392
- 3
- 9
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
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/229608.webp)
- 957
- 3
- 12
- 19
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
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/159698.webp)
- 398
- 3
- 9
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
![](../../users/profiles/1231241.webp)
- 1,173
- 14
- 17
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.
![](../../users/profiles/7676971.webp)
- 1,430
- 1
- 18
- 24
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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/3171.webp)
- 179,733
- 56
- 298
- 296
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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/6275842.webp)
- 65
- 1
- 1
-
done this on a linux server, the file had `^M` endings. save as dos to keep, save as linux to remove. can check using `cat -v` – HattrickNZ May 11 '16 at 20:58
-
1
: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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/2974.webp)
- 34,617
- 13
- 76
- 144
-
Elaborated in [Dannid's answer](https://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim/11475637#11475637). – Peter Mortensen Aug 29 '20 at 14:07
To run directly in a Linux console:
vim file.txt +"set ff=unix" +wq
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/1274699.webp)
- 595
- 4
- 13
- 28
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
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/1388584.webp)
- 313
- 1
- 4
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}";
}
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/7780.webp)
- 9,895
- 35
- 70
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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/1971057.webp)
- 942
- 1
- 8
- 15
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
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/6437949.webp)
- 11
- 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
![](../../users/profiles/992687.webp)
- 23,164
- 22
- 90
- 133
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.
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/6680821.webp)
- 777
- 1
- 11
- 19
-
[dos2unix](https://en.wikipedia.org/wiki/Unix2dos) is not installed by default on e.g. [Ubuntu 20.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_20.04_LTS_(Focal_Fossa)) (Focal Fossa). – Peter Mortensen Aug 29 '20 at 16:03
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>
![](../../users/profiles/63550.webp)
- 28,342
- 21
- 95
- 123
![](../../users/profiles/4696986.webp)
- 29
- 3