How to view and change the newline markers of a file in Vim

It is pretty common to create, edite and exchange source files between Windows, Linux and Mac computers. The newline markers in text files are different on all these three systems. Sometimes, this leads to funny looking characters when files are created on one system and opened on another. Typically, the dos2unix and unix2dos tools are used to convert text files between these formats.

In Vim, it is easy to convert the end-of-line markers of a file to that of Windows, Linux or Mac. This is done using the fileformat option.

To view the end-of-line markers of the currently open file:

:set fileformat?

To set the end-of-line markers of the file to Windows:

:set fileformat=dos

To set the end-of-line markers of the file to Linux:

:set fileformat=unix

To set the end-of-line markers of the file to Mac:

:set fileformat=mac

After changing the newline markers, save the file.

Tried with: Vim 7.3 and Ubuntu 12.04 LTS

One thought on “How to view and change the newline markers of a file in Vim”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s