3

This is driving me nuts. Similar SO questions don't contain the answer, though, so here it goes again in slightly different form:

Is there a way to:

  1. Make vim show 0x0a at the end of file as a blank line?

  2. Supposing #1 can't be done, how do I delete the eol? There is no line, so there is nothing to delete.

For example:

  1. vim -b myfile (currently no eol)
  2. Add blank line at the end of file, :w :q
  3. vim -b myfile - the blank line is gone, but hexdump shows 0x0a is still there. This is inconsistent behaviour.
Yuri Geinish
  • 16,744
  • 6
  • 38
  • 40

3 Answers3

4

You can use set noeol in binary mode:

:help noeol

   When writing a file and this option is off and the 'binary' option
    is on, no <EOL> will be written for the last line in the file.  This
    option is automatically set when starting to edit a new file, unless
    the file does not have an <EOL> for the last line in the file, in
    which case it is reset.

see also: Vim show newline at the end of file

Community
  • 1
  • 1
perreal
  • 94,503
  • 21
  • 155
  • 181
  • How do I know the eol is there or no? It cannot be seen unless I use `set line` and then doing `set noeol` is inconvenient and it also requires `:w`, as otherwise it won't do anything. Isn't there a normal way? I've used vi/vim for the last 20 years, and this is the first time I'm frustrated with it. – Yuri Geinish Apr 29 '14 at 08:43
  • @YuriGeinish - you can use the cli tool `od -x | tail` to see it. It'll show up as a `000a` in `od`'s output. – slm Jan 20 '18 at 04:08
  • If vim is installed xxd should be too, which is a much more helpful hex dump that does what I want by default. – Ed Neville Jan 24 '18 at 21:32
4

I don't know how to see the last blank line, but to remove just open your file, the run:

:set binary noendofline

This will remove the last (invisible) blank line from it.

Warning: because of binary some settings will be modified (for example textwidth)!

bimlas
  • 2,359
  • 1
  • 21
  • 29
1

The way Vim shows 0x0a at the end of the file is that it opens the file without complaining about [noeol] when :editing the file (in a kind of "reverse logic" from what you expect). As you've probably read already, Vim's (and Unix) philosophy is that the trailing newline should be there.

Based on this philosophy, I wouldn't recommend intentionally creating files without a trailing newline. However, there are ways to make Vim respect and maintain such existing files. My PreserveNoEOL plugin provides a way to do this effortlessly.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I'm OK with editing it in binary mode, in which case Unix's philosophy does not apply. The problem is, even in binary mode, vim hides the trailing 0x0a and there is no way to see it and delete without using `set line` to check and then `set noeol` and `:w` to remove. – Yuri Geinish Apr 29 '14 at 08:41
  • 1
    Well, you could add `'eol'` to the `'statusline'` setting (to show you whether one will be written), or even build a real representation in the buffer (using `:autocmd BufWritePre / BufWritePost` to append an empty line / remove it during buffer save). – Ingo Karkat Apr 29 '14 at 09:46