17/01/2015

[Vim] Remove empty lines from file

Once you learn the magic :q! combination to close Vim discarding changes, you find out that it is a quite powerful tool.

To remove empty lines from a file you might try with some specific commands such as:

:g/^$/d

Where g tells vim to execute a command only on the lines that match a regular expression, ^$ is said regular expression to match empty (blank) lines, and d is the delete command.

Bonus: if when you open the file you see a lot of ^M characters, it means you're editing it in Unix format but the file was created in DOS format. You can either try by telling vim to treat it as a DOS format before running the previous instruction:

:set ff=dos

or by converting the file to Unix format beforehand with the dos2unix command:

dos2unix -n in out

No comments:

Post a Comment

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.