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

How do I save a file with specific line endings in one command without changing the buffer's fileformat setting?

Answer

:w ++ff=unix

Explanation

The ++ff modifier on :w forces the file format used for writing, independently of the buffer's 'fileformat' option. This is useful when you need to produce a file with a specific line ending (Unix LF, Windows CRLF, or Mac CR) without permanently altering how Vim treats the buffer.

How it works

  • :w — write the buffer to disk
  • ++ff=unix — override the file format for this write only; unix writes LF line endings, dos writes CRLF, mac writes CR
  • The buffer's 'fileformat' setting is not changed, so subsequent writes without ++ff continue using the original format

Example

You're editing a file that Vim detected as dos (CRLF), but you need to deliver it as Unix LF without permanently flipping the buffer:

:w ++ff=unix output_unix.txt

This writes a clean Unix copy to output_unix.txt while the current buffer remains dos.

To convert the buffer itself, use :set fileformat=unix before writing:

:set fileformat=unix
:w

Tips

  • Combine with a filename to write to a different path: :w ++ff=unix converted.sh
  • Works with any write variant: :saveas ++ff=unix newfile.txt
  • The sibling ++enc=utf-8 modifier does the same for character encoding
  • Check the current buffer's format with :set ff?

Next

How do I open the directory containing the current file in netrw from within Vim?