59

Sometimes I want the last line of a text file to be a simple string of text with no newline character appended: ...eg. to concatenate another file to it.

Several editors automatically modify my text by adding a newline character, even though I have not pressed Enter.

Remains unmodified: Emacs, SciTE, Kate, Bluefish, Notepad(wine)

Newline is added: Gedit, Gvim, Vim, Nano

I would like to know if there is some way to tweak Gedit, Gvim, and Vim to only save what I have typed.... and even Nano might come in handy

I currently use Gedit, and I'm dabbling in Gvim/Vim.. so it would be useful to know how to "toggle" these on/off...

PS. Upon re-opening the same file in Gedit, Gvim, and Vim, the cursor's end-of-file placement is at the end of the text, whereas it should really show up on the next (empty) line. This is misleading (but that's because of what I'm used to).

I used a hexeditor to check the above observatons.

3 Answers 3

64

For gEdit there does not seem to be a way to disable having a newline inserted at the end see this answer.

For Vim (and gVim) you can - by executing the following option: :set binary. Setting to binary will save the file as is and not insert a newline at the end of the document (Unless there already is one, in which it will be retained). At anytime you can revert this option with :set nobinary.

Nano is a little easier. You can launch nano with the -L flag (or --nonewlines if you're more the visual type). This will disable the automatic addition of the newline to the end of the file (so long as one was not actually entered). Ideally you could setup this alias in your ~/.bashrc: alias nano="nano -L", which would launch Nano with the no newlines flag included in it!

Since I don't know exactly what you're doing I'm going to point you to Why should files end with a newline? which may explain why they're there. In short, some applications will fail if they don't find an EOL directly before the EOF.

6
  • 1
    Thanks Marco, a great answer...The "Why shoulds..." is an interesting collection of "whys"... I'm personally very "newline aware" (that's why I noticed it :) but it is certainly a good reference for everyone (myself included)... but I do prefer to know that I added, or did not add it... Maybe there is an obscure plugin for Gedit...
    – Peter.O
    Nov 15, 2010 at 6:02
  • 3
    It seems that for Gvim/Vim it requires two options to be set... as per :help eol ... When writing a file and this option ('eol') is off and the 'binary' option is on, no EOL will be written for the last line in the file.
    – Peter.O
    Nov 15, 2010 at 8:25
  • 1
    Gedit (Launchpad) is aware of this issue, and have it on the "wishlist" .... (I wish, but I won't hold my breath for this one! if this extract means anything: "...(it) would rather confuse users"
    – Peter.O
    Nov 15, 2010 at 12:03
  • 3
    For nano, you can put set nonewlines in ~/.nanorc (or /etc/nanorc) to use -L mode by default.
    – mwfearnley
    Sep 20, 2016 at 13:50
  • Use editorconfig (editorconfig,org) - plugins available for vim and gedit. Specify desired options in your .editorconfig file.
    – ddbug
    Sep 14, 2018 at 1:54
17

For gEdit:

If you are using Ubuntu 12.04 or above, you can set the behavior already.

Through a graphical user interface:

  1. Run dconf Editor (dconf-editor)
  2. Go to orggnomegeditpreferenceseditor
    (or just search for ensure-trailing-newline)
  3. Uncheck ensure-trailing-newline if you don't want automatically added newlines.

On the command line:

  1. Open a terminal (ctrl+alt+T)
  2. enter the following command

    gsettings set org.gnome.gedit.preferences.editor ensure-trailing-newline false
    

    and you're done.

Reference | Launchpad bug

2
  • 1
    Thanks. That's good to know. I've tried it and it works... (+1)
    – Peter.O
    Dec 5, 2013 at 23:24
  • what about pluma editor? (don't see "ensure-trailing-newline" for pluma)
    – acgbox
    Jun 20, 2018 at 22:01
0

If you just want to write a single line file, then echo is probably the easiest solution:

echo -n "the contents of my file" > myfile
1
  • For clarity, the -n flag tells echo to not print the trailing newline character.
    – tww0003
    Nov 27, 2019 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .