vimtricks.wiki Concise Vim tricks, one at a time.

How do I convert a file from Windows CRLF to Unix LF line endings in Vim?

Answer

:set fileformat=unix

Explanation

When you open a Windows file in Vim on a Unix system, you may see ^M at the end of every line — that's the carriage return (\r) from CRLF line endings. Setting the fileformat option tells Vim how to write line endings when the file is next saved, cleanly converting them.

How it works

  • fileformat (abbreviated ff) controls the line-ending style Vim uses when writing the buffer to disk
  • unix — LF only (\n), the standard on Linux and macOS
  • dos — CRLF (\r\n), used on Windows
  • mac — CR only (\r), legacy Classic Mac OS
  • Setting ff=unix marks the buffer as modified; :w then re-saves with the new line endings

Example

:set fileformat=unix
:w

After saving, the file has Unix line endings. You can verify with :set ff?, which should now show fileformat=unix.

Tips

  • Check the current setting at any time with :set ff?
  • To force Vim to read an already-open file with a specific format (useful when auto-detection guesses wrong): :e ++ff=dos
  • To convert the other direction — Unix to Windows — use :set ff=dos then :w
  • The fileformats option (:set ffs) controls the order Vim uses when auto-detecting on open

Next

How do I duplicate every line matching a pattern, placing a copy directly below each one?