Sometimes when I’m working with files in Git, I find myself with a change in a file that says something like
\ No newline at end of file
This happens because the file had no newline at the end of the file but when I modify the file in my text editor the newline is added on save.
Why You Should Have a Newline at the End of the File
Officially, the Posix standard defines a line as
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.
How to Remove the Newline at the End of the File
Even though the newline should appear at the end of the file, sometimes I’ll opt to remove it to minimize the “noise” in a commit. I’ve found the easiest way to do this is with
perl -pi -e 'chomp if eof' <myfile>
perl -pi -e 'chomp if eof' composer.json
You can create an alias called
chompeof by adding the following to your shell startup file (e.g.
alias chompeof="perl -pi -e 'chomp if eof'"
See my Pull Request Add alias to remove newline at end of file to add this to my Zsh configuration.
Now I can use