brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set ff=unix in Vim removes Windows line endings

Vim
~/tips/tip-vim-fileformat-unix

:set ff=unix

Converts a file with Windows line endings back to Unix ones.

A shell script that came from Windows fails with odd errors like bad interpreter and a ^M. The line endings are the problem, and Vim fixes them in one line.

:set ff?
:set ff=unix
:w

ff is short for fileformat. The first command shows what Vim detected: dos means every line ends in a carriage return and a newline. Setting it to unix and saving writes the file with plain newlines. The text itself doesn’t change.

Vim hides the carriage returns when it detects a DOS file, so the file looks normal until you check this option.

Files with mixed line endings are a different story. Vim reads those as unix and shows ^M at the end of some lines. A substitute removes them:

:%s/\r$//

The other direction works too: :set ff=dos before saving gives a file that old Windows tools like.

For a batch of files I’d use dos2unix or sed in the shell instead. And if the file is in Git, check core.autocrlf, since Git may convert the endings back on the next checkout.

Further reading: Vim help: 'fileformat', Vim help: file formats

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link