0

I'm trying to write a Vim command script (a file containing Ex commands) that removes all existing lines in a file (and then read in some new text, and do some further editing operations). I use the command % delete _ which generates the error "E749: empty buffer" and eventually when Vim terminates produces exit code 1.

I tried prefixing the command with :silent! and I also tried putting it in a :try ... :catch ... :endtry segment, but neither of those made any difference.

EDIT: Here's a simple example:

new
append
hello world
.
%delete_

I put that into a file called "sc" and run it with vim -es <sc (with or without -V1).

6
  • 1
    E749 takes me to :print in the help. Running :new | %delete _ I did not get any errors. Can you edit with exact steps to reproduce?
    – D. Ben Knoble
    May 10, 2022 at 19:33
  • 1
    Thanks, @D.BenKnoble. Added an example. Also, yes, I noticed that help took me to :print, but I couldn't understand why (it doesn't seem to have anything to do with printing).
    – user98761
    May 10, 2022 at 20:36
  • Welcome to Vi and Vim! Does it help if you add a space in between the delete and the _? I think that might be the problem.
    – filbranden
    May 10, 2022 at 20:52
  • 1
    @filbranden No, adding a space doesn't seem to make any difference.
    – user98761
    May 10, 2022 at 21:09
  • Yes I can reproduce the issue with your instructions. Interesting!
    – filbranden
    May 10, 2022 at 22:07

1 Answer 1

0

You are getting this error from running in Ex-mode, which in your case you're getting through passing Vim the -e command-line argument, but you could equally get there by running the ex command directly (instead of vim), or using the Q Normal-mode command to enter Ex-mode.

The error message is coming from this line in do_exmode(), which implements the Ex-mode loop. After running an Ex command, it will perform some checks and raise error E749 if the buffer is empty.

Digging into history of this code, it seems this behavior was introduced in Vim version 7.0051, from February 2005, so it's been this way for a long time.

Looking at the code around that line, there's another error that might be raised there, which is E501: At end-of-file, which is raised if you press enter on the Ex prompt (i.e. :<CR>) while you're at the last line of the buffer. Pressing enter on the Ex prompt will advance you to the next line. In light of that behavior, it makes sense that doing so at the end of the buffer will raise an error.

An empty buffer is, in a sense, a corner case of that, since technically you're at or past the last line of the buffer, even though you're also at the first and only line. That probably justifies raising an error in that situation, and the authors probably picked E749: empty buffer as it's more descriptive than the E501 error.

There doesn't seem to be a workaround for this issue. Maybe you can avoid using Ex-mode? Maybe you can avoid the situation where the buffer gets completely empty? But indeed :silent! or :try won't help, because the error is not really coming from the :delete command, but from the Ex-mode loop, after the command has emptied the buffer.

4
  • 1
    So, Ex raises an error when any command leaves the buffer empty? That sounds odd…
    – D. Ben Knoble
    May 11, 2022 at 2:36
  • 1
    Seems to be corrected, but I can't find from what version.
    – gildux
    Feb 5 at 3:59
  • @gildux Which version are you testing? I just checked Vim 9.0.981 and I can still reproduce the behavior. Are you testing this on Vim or Neovim?
    – filbranden
    Feb 6 at 18:34
  • 1
    Vim 9.0 (2022 June 28) including patches 1-639. I've launched ex directly, typed new<CR> without error, typed append<CR> then hello world<CR>.<CR> without error, typed %delete_<CR> and had a new-line as output. To be sure, I tried %list<CR> and it said E749: Empty buffer as expected. Last, I typed quit<CR> and it said E37: No write since last change (add ! to override) of course. But q!<CR> showed in it's place [e] <empty buffer> hmm… Another q!<CR> let me exit however. imgur.com/a/aoTse6E
    – gildux
    Feb 6 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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