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(abbreviatedff) controls the line-ending style Vim uses when writing the buffer to diskunix— LF only (\n), the standard on Linux and macOSdos— CRLF (\r\n), used on Windowsmac— CR only (\r), legacy Classic Mac OS- Setting
ff=unixmarks the buffer as modified;:wthen 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=dosthen:w - The
fileformatsoption (:set ffs) controls the order Vim uses when auto-detecting on open