[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


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.


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


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

:%s/\r\+//g

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


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


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


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

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


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

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.


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

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.


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.


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


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


To run directly in a Linux console:

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

You can use:

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

Or the dos2unix utility.


To run directly in a Linux console:

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

You can use:

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

Or the dos2unix utility.


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


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


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


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.


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

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.


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

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

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.


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

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.


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

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

Questions with linux tag:

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? How to post raw body data with curl? Copy Files from Windows to the Ubuntu Subsystem How to use local docker images with Minikube? Can Windows Containers be hosted on linux? gradlew command not found? ssh connection refused on Raspberry Pi Composer: file_put_contents(./composer.json): failed to open stream: Permission denied Curl : connection refused boto3 client NoRegionError: You must specify a region error only sometimes gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now sudo: docker-compose: command not found How to upgrade pip3? How can I remove jenkins completely from linux Linux Command History with date and time MySQL fails on: mysql "ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded" What is difference between arm64 and armhf? How to redirect output of systemd service to a file Retrieve last 100 lines logs Failed to find Build Tools revision 23.0.1 Run an Ansible task only when the variable contains a specific string What does `set -x` do? How to edit a text file in my terminal Starting a shell in the Docker Alpine container How to run SUDO command in WinSCP to transfer files from Windows to linux Fail during installation of Pillow (Python module) in Linux How to install Android SDK on Ubuntu? How do I delete virtual interface in Linux? What is the default root pasword for MySQL 5.7 Docker command can't connect to Docker daemon How to find files modified in last x minutes (find -mmin does not work as expected) Can I use Homebrew on Ubuntu? Pycharm and sys.argv arguments Ubuntu: OpenJDK 8 - Unable to locate package Fork() function in C Amazon Linux: apt-get: command not found Ionic android build Error - Failed to find 'ANDROID_HOME' environment variable Ubuntu: Using curl to download an image Docker error response from daemon: "Conflict ... already in use by container" Curl command without using cache Can't get private key with openssl (no start line:pem_lib.c:703:Expecting: ANY PRIVATE KEY)

Questions with vim tag:

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? How to replace space with comma using sed? Git commit in terminal opens VIM, but can't get back to terminal vi/vim editor, copy a block (not usual action) How do I switch between command and insert mode in Vim? How to go back (ctrl+z) in vi/vim Vim: How to insert in visual block mode? How do I exit the Vim editor? Vim multiline editing like in sublimetext? How to make vim paste from (and copy to) system's clipboard? How to add text at the end of each line in Vim? Where is my .vimrc file? How to open a new file in vim in a new window vim line numbers - how to have them on by default? Go to beginning of line without opening new line in VI Vim: insert the same characters across multiple lines Setting up Vim for Python How do I exit from the text window in Git? How to copy selected lines to clipboard in vim How can I convert spaces to tabs in Vim or Linux? How to cut an entire line in vim and paste it? How can I quickly delete a line in VIM starting at the cursor position? How I can delete in VIM all text from current line to end of file? Is it possible to interactively delete matching search pattern in Vim? Vim: faster way to select blocks of text in visual mode How to get the list of all installed color schemes in Vim? Update built-in vim on Mac OS X Vim autocomplete for Python "Find next" in Vim Using git commit -a with vim How to specify a editor to open crontab file? "export EDITOR=vi" does not work What is the difference between MacVim and regular Vim? What does ^M character mean in Vim? How to use vim in the terminal? Go to first line in a file in vim? What are the most-used vim commands/keypresses? A more useful statusline in vim? vim - How to delete a large block of text without counting the lines? How to expand/collapse a diff sections in Vimdiff? Autocompletion in Vim How to view UTF-8 Characters in VIM or Gvim

Questions with file tag:

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? How to convert Base64 String to javascript file object like as from file input form? Is the MIME type 'image/jpg' the same as 'image/jpeg'? TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3 Find a file by name in Visual Studio Code How to rename a directory/folder on GitHub website? importing external ".txt" file in python How to open local files in Swagger-UI How to compare two files in Notepad++ v6.6.8 How to create an empty file with Ansible? How to download file in swift? How to save a list to a file and read it as a list type? Sending a file over TCP sockets in Python Write variable to a file in Ansible How to open .SQLite files Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? How do I delete files programmatically on Android? Javascript loading CSV file into an array Reading string by char till end of line C/C++ How to use Javascript to read local text file and read line by line? Flask raises TemplateNotFound error even though template file exists How to get File Created Date and Modified Date Open files in 'rt' and 'wt' modes python requests file upload FileNotFoundError: [Errno 2] No such file or directory How does Java resolve a relative path in new File()? download file using an ajax request Writing to a new file if it doesn't exist, and appending to a file if it does Reading a resource file from within jar Reading numbers from a text file into an array in C Call a function from another file? How can I use a batch file to write to a text file? Reading from file using read() function Basic http file downloading and saving to disk in python? Save byte array to file Append to the end of a file in C How to save a dictionary to a file? Encode a FileStream to base64 with c# Compare two files report difference in python Compare two different files line by line in python how to count the total number of lines in a text file using python

Questions with editor tag:

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? How to make vim paste from (and copy to) system's clipboard? How to replace four spaces with a tab in Sublime Text 2? How do I force Sublime Text to indent two spaces per tab? Setting up Vim for Python How to take off line numbers in Vi? How to make HTML table cell editable? Go to first line in a file in vim? Any good, visual HTML5 Editor or IDE? How do I make Git use the editor of my choice for commits? How to comment out a block of Python code in Vim Turning off auto indent when pasting text into vim How to Select Columns in Editors (Atom,Notepad++, Kate, VIM, Sublime, Textpad,etc) and IDEs (NetBeans, IntelliJ IDEA, Eclipse, Visual Studio, etc) Copy all the lines to clipboard How do I indent multiple lines at once in Notepad++? Differences between Emacs and Vim What LaTeX Editor do you suggest for Linux? GUI-based or Web-based JSON editor that works like property explorer Disabling swap files creation in vim Does Notepad++ show all hidden characters? Open two instances of a file in a single Visual Studio session What are the dark corners of Vim your mom never told you about? What are the benefits of learning Vim? Indent multiple lines quickly in vi Is there a good JSP editor for Eclipse? Text editor to open big (giant, huge, large) text files How to copy and paste code without rich text formatting? How do I move to end of line in Vim? Best Free Text Editor Supporting *More Than* 4GB Files? Using Vim's tabs like buffers Best C++ IDE or Editor for Windows Convert DOS line endings to Linux line endings in Vim What IDE to use for Python? How to duplicate a whole line in Vim? JavaScript editor within Eclipse List of macOS text editors and code editors What Ruby IDE do you prefer? How can I set up an editor to work with Git on Windows? Text Editor For Linux (Besides Vi)?

Questions with dos2unix tag:

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