martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.26k stars 258 forks source link

Don't create new empty files #956

Closed mcepl closed 2 years ago

mcepl commented 3 years ago

Quite often I use command vis *.spec (my primary work is with RPM SPEC files), and too often I run this command in a wrong directory. The result is that I have all over the system files called *.spec and *.changes (with asterisks in the filename) with zero length.

Would it be possible to add to vis a herusitics that it will never CREATE new ZERO-LENGTH file? I can hardly imagine a situation where using $EDITOR for creating zero-length files would be preferable to touch filename or cat /dev/null >filename.

mizlan commented 3 years ago

It seems you can use the nullglob option or the equivalent for your shell, which I think makes more sense than trying to get vis to detect it. Also, the heuristic you mention seems unnecessary and unintuitive. I sometimes save empty files and there's nothing wrong with that.

mcepl commented 3 years ago

It seems you can use the nullglob option or the equivalent for your shell, which I think makes more sense than trying to get vis to detect it.

OK, I didn't know that. I will try it.

I sometimes save empty files and there's nothing wrong with that.

Yes, but then you run :w explicitly, it is not just because you pressed ZZ on empty buffer.

mizlan commented 3 years ago

ZZ effectively means "save and exit" so it is explicitly saving the empty buffer. In this case, I think what you're looking for is ZQ, which will not save the file.

Take a look at the source code:

https://github.com/martanne/vis/blob/1a958f221404b09cb8b0612fb34301e6b9783cf9/config.def.h#L231

https://github.com/martanne/vis/blob/1a958f221404b09cb8b0612fb34301e6b9783cf9/config.def.h#L234

mcepl commented 3 years ago

@martanne I guess, it really doesn't matter what we write here. What do you think? Making vis more for mere humans (and Zenclavier) or keeping vis just a platonic idea without regards to its usefulness for mere humans?

ninewise commented 3 years ago

You could also remap ZZ to a lua function which will drop empty files, actually, as easily as adding the following to your visrc:

vis:map(vis.modes.NORMAL, "ZZ", function()
    if vis.win.file.size == 0 then
        vis:command(':q!')
    else
        vis:command(':wq')
    end
end)

Also I'm a mere human and using vis.

mizlan commented 3 years ago

I think the way Vim does it is ZZ only writes if the buffer has been modified, which would also work in this case (also would be a more sensible option than a special case for empty files).

ninewise commented 3 years ago

Hm that does make more sense... that could be default behaviour, here, too.

vis:map(vis.modes.NORMAL, "ZZ", function()
    if vis.win.file.modified then
        vis:command(':wq')
    else
        vis:command(':q!')
    end
end)
mcepl commented 2 years ago

vis:map(vis.modes.NORMAL, "ZZ", function()
  if vis.win.file.modified then

Wouldn’t this be true even if undo my changes or delete them afterwards? file_size==0 seems like more foolproof solution.

mizlan commented 2 years ago

If you want to emulate Vim behavior, this has nothing to do with the size of the file. In Vim, ZZ is equivalent to :x, which acts like :wq if the buffer is modified and :q if the buffer is not. Anyway, if you were to originally have a file with contents, and then delete the file contents, and then choose to save that file, what logically would follow is an empty file.