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).

  • 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: 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
  • 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. – lunaryorn Sep 29 '14 at 15:47

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)

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).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.