Extra next line in the end of file

Asked by Märt Suga

Gedit add extra next line tag to the end of each file. Gedit does not show this \n tag, but other editors show it. Is there a way to disable this "feature"?
Gedit version is gedit 2.18.1 (Feisty)

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gedit Edit question
Assignee:
No assignee Edit question
Solved by:
Cesare Tirabassi
Solved:
Last query:
Last reply:
Revision history for this message
Best Cesare Tirabassi (norsetto) said :
#1

gedit always add a line feed at the end of a text file:

https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=75831

This is not customisible. You can always modify the sorce code if you so wish.
The relative code is in gedit-document-saver.c lines 262 to 291:

 if (res)
 {
  if (encoding != gedit_encoding_get_utf8 ())
  {
   gchar *converted_n = NULL;
   gsize n_len;

   converted_n = gedit_convert_from_utf8 ("\n",
              -1,
              encoding,
              &n_len,
              NULL);

   if (converted_n == NULL)
   {
    /* we do not error out for this */
    g_warning ("Cannot add '\\n' at the end of the file.");
   }
   else
   {
    written = write (fd, converted_n, n_len);
    res = ((written != -1) && ((gsize) written == n_len));
    g_free (converted_n);
   }
  }
  else
  {
   res = (write (fd, "\n", 1) == 1);
  }
 }

Revision history for this message
Märt Suga (mart-suga) said :
#2

Thanks Cesare Tirabassi, that solved my question.

Revision history for this message
Märt Suga (mart-suga) said :
#3

This solved my problem, but I hope this will be a customizable feature in the next version because it is very important to end php files with ?> instead of /n.

Revision history for this message
Cesare Tirabassi (norsetto) said :
#4

If nobody asks for it I doubt it will. Why not raising a bug/wish in Gnome?

http://bugzilla.gnome.org/

You can also issue a bug report in Launchpad and link the upstream bug report to it.

Revision history for this message
Jason Miller (millermobile) said :
#5

Actually, having an extra \n character at the foot of a PHP document has no effect on output.
At the very most, if you are programming for the web using PHP, you will see an extra line in your output's source code, that is obviously invisible when rendered in HTML.

If you are concerned with passing a \n character amogst your AJAX response from PHP, it won't affect your client-side script, assuming you have double-quoted your eval statement.

Revision history for this message
Andrius (andrius-optimalgroup) said :
#6

millermobile is correct about that extra \n not having an effect on output. Although it works differently.

PHP ignores extra end of line after "?>".
http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php

But extra \n are sometimes visible in HTML.

Consider <img /><img />, you will get too images with no space between them,
but if you have <img />\n<img /> you will get a space.
Also there is <pre> element.

Revision history for this message
Märt Suga (mart-suga) said :
#7

My development team standards require to end files with "?>" not "?>\n" so every time a new version comes out I have to modify and compile it. Also I do not understand why is it disabled to modify a text file precisely the way I need it to be - isn't this the whole point of a text editor?

Revision history for this message
Jason Miller (millermobile) said :
#8

Andrius - Although this is correct, I have never found myself ending a PHP file in the middle of two images...
The <pre> situation is surely a common problem - I believe I may have ran into this some time ago and just modified the CSS for that <pre> element to chop 1em off the bottom.

-Jason

Revision history for this message
HolySmoke (7-admin-localhorst-tv) said :
#9

Actually it is rather important that php files end with exactly ?> without trailing characters, even whitespace since this is considered "output" thus making it impossible to send any header lines after inclusion of such a file.

Still, though, I haven't had any problems with trailing newline characters in gedit so far so this might be caused by a plugin or this bug is fixed already.

Revision history for this message
Tchalvak (tchalvak) said :
#10

As HolySmoke says, the additon of the newline at the end is problematic with php programming because of need for redirects to occur without output being sent. More to the point, just opening a file with gedit modifies the file if there isn't a trailing newline at the end of the file. That's disruptive.

Revision history for this message
akuma-kun (akuma-kun) said :
#11

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 already explained here by Tchalvak on 2009-03-02.