12

One of vim's quirks is that it doesn't show an empty line at the end of a file if the file ends with the newline character. This way, if the file doesn't end with a newline, the display is the same, only with the [noeol] marker in the modeline.

Emacs on the other hand, displays the last line if the file ends with a newline, and doesn't otherwise.

While Emacs' behaviour is fundamentally more sensible, for better or worse, I've gotten used to the former.

Is there any way to suppress the display of the last empty line, while still requiring that the file be newline terminated?

Emacs new-line behaviour

The screenshot shows three windows (view image in new tab if too small):

  1. Large emacs window on the left in hexl-mode, showing you that there really is a newline at the end of the file (0a, where the cursor is positioned).

  2. Emacs window on top right, with cursor on the empty third line.

  3. Vim window on bottom right, with cursor on the last text line. The cursor cannot move below this point, even though the file ends with a newline (there is no [noeol] marker on the vim statusline).

sykora
  • 269
  • 2
  • 10
  • How is the newline displayed? Does it display because you have some minor mode enabled? It will be useful to put a snippet from your setup that displays the last newlines. A screenshot of that would be useful too. – Kaushal Modi Sep 29 '14 at 14:05
  • 2
    By "display", I don't mean that there's a visual representation of the newline. I simply mean that I can move my cursor to that line. – sykora Sep 29 '14 at 14:12
  • Unable to reproduce: http://imgur.com/IAoV7p9 Same behavior with `emacs -Q`, it must be something in your init file. – T. Verron Sep 29 '14 at 14:33
  • Or wait... you mean that you don't want to be able to move to that line, even though the line exists? I second @kaushalmodi , screenshots of the vim and the emacs behavior would be useful. – T. Verron Sep 29 '14 at 14:36
  • @T.Verron Yes, that's what he meant I think. His problem is with the behavior, not appearance. – Malabarba Sep 29 '14 at 14:44
  • But then how can you add a blank line, then text at the end of a file? – T. Verron Sep 29 '14 at 14:50
  • 3
    @sykora Please try to use **minimal** screenshots. There is a **lot** of excess and redundant blank space in the screenshot, making it much larger than it needs to be, which renders the thumbnail almost useless, since it's too small to see what is going on. **When taking screenshots, please resize windows to fit their contents.** –  Sep 29 '14 at 15:47
  • @T.Verron I believe that the sykora wants a setup where the files are saved with at least one blank line at the end. If he wants to enter a new line to type at the end, he would have 2 newlines at the EOF. If he inserts a blank line and then starts at the next line, he will have 3 newlines at the EOF. This is possible by `require-final-newline` but sykora wants that last newline to be always be uneditable/unremovable. – Kaushal Modi Sep 29 '14 at 16:02
  • So, basically, something that adds a newline when saving a file, and removes the last newline when opening a file. – T. Verron Sep 29 '14 at 17:28

2 Answers2

2

Let me give a horrible suggestion to implement that:

(defun make-eof-intangible ()
  (interactive)
  (add-hook 'post-command-hook (lambda ()
                                 (when (and (equal (point) (point-max))
                                            (looking-back "\n"))
                                   (backward-char))) t t))

(I'm using a local hook because I suspect setting it globally might mess up things seriously, so better enable it on a per-buffer basis)

Sigma
  • 4,510
  • 21
  • 27
1

These are probably not exactly what you want, but may be useful.

  • I'm using the linum-mode minor mode in all my buffers (set (global-linum-mode t) in your init file). This shows line numbers only for the lines that have a newline character at the end.

  • Another thing you might like is enabling whitespace-mode, by default this shows grey $ signs on empty lines, thus allowing you to see which lines are empty. See the Emacs Wiki page here.

  • In a similar way, setting indicate-empty-lines to t will show you where the last line is (see the manual for fringe indicators here).

ph0t0nix
  • 1,119
  • 13
  • 28