LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Extra newline at end of file? (http://www.linuxquestions.org/questions/linux-software-2/extra-newline-at-end-of-file-718461/)

sayeo87 04-11-2009 01:27 PM

Extra newline at end of file?
 
Hi,

I've noticed when I create a generic textfile in vim and have a trailing newline at the end of the file, when I open it with kate it has 2 new lines at the end of the file instead! This is on a fedora machine. The same behavior is observed when I copy the file over to my windows machine and open it in notepad++ (which is able to handle both unix and dos newlines) - 2 newlines instead of 1.

Feeling curious, I tried creating a textfile in vim on my ubuntu machine and used gedit to open it, but surprisingly gedit displayed it correctly - only 1 newline.

Would anybody mind explaining this oddity?


Thanks!

1337 04-12-2009 07:09 AM

It would appear that this is a bug in kate. I wrote a script which reads a file into a byte array and then prints the hex value of those bytes and there is no extra /n inserted (0x0A), however in kate it is showing an extra new line no matter what i do. Maybe someone should submit a bug report?

sayeo87 04-12-2009 11:00 AM

Hmmm, I don't know. What about the fact that Notepad++ in Windows has the same behavior? The inverse is also true: if I create a file in Notepad++ under Windows and switch it to linux format with 2 newlines at the end, when I move it to a linux machine and open it with vim I only see 1 newline at the end....

Raumkraut 04-24-2009 04:06 AM

Vim and Gedit silently add an extra newline when you use them to save a file. They also hide any final newline from the user, like a dirty secret.
As hinted at, you can expose this behaviour by viewing the file - and the extra newline - in a less deceitful editor: kate, nano, emacs, etc.

Libu 04-24-2009 04:21 AM

gedit 2.16 shows the extra new line. Wonder why vi creates/display's it?

jlinkels 04-24-2009 05:07 PM

It is a documented vi feature to add a newline to the end of the file.

There are some programs (cron being the most notoriously) which must have a newline at the end of the file. Maybe it is something from the old Unix days where each file had to have a newline at the end to get processed correctly. I am not sure whether that was customary those days.

jlinkels

RoboRecht 10-30-2009 05:16 PM

in vim
 
In vim you can type
:set noeol
:set binary

3r0s 09-17-2010 04:45 AM

I tried LeafPad on my Ubuntu Linux and it works like Windows NotePad: no extra new line at the end of the file.
There are people saying that Windows NotePad is one of the worst text editor, but i would say that it just serves the simple purpose to edit simple basic text with no frills and without adding any extra.
On Ubuntu Linux with GNOME desktop i would suggest to use Gedit for general purpose text editing, and LeafPad for editing PHP, HTML, CSS, JavaScript, etc. files.
The importance of not having this extra new line is described at launchpad+question+8787


All times are GMT -5. The time now is 01:13 PM.