zed icon indicating copy to clipboard operation
zed copied to clipboard

Saving an empty file creates an additional new line

Open joshuaia opened this issue 1 year ago • 6 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

  1. Create and open new empty file, for example vim.txt
  2. Don't type anything, just save the file
  3. File will be saved with two new lines inside

Same behaviour in vim mode.

I think there shouldn't be multiple new lines in an empty file.

Environment

Zed: v0.132.2 OS: macOS Sonoma 14.4.1 Memory: 16GB Architecture: Apple M2

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

No response

joshuaia avatar Apr 26 '24 08:04 joshuaia

I don't see this behaviour on 0.133.2

Imgkl avatar Apr 26 '24 08:04 Imgkl

Small update, it's important to have ensure_final_newline_on_save=true.

joshuaia avatar Apr 26 '24 08:04 joshuaia

Just so we're clear, are there two newlines or just two lines with a single newline?

This is what I see:

Screenshot 2024-04-26 at 11 05 32 AM

The above behavior is the expected behavior when ensure_final_newline_on_save is true.

maxdeviant avatar Apr 26 '24 15:04 maxdeviant

Yes, your reproduction is correct. I was expecting no newline without content, for example SublimeText doesn't create a newline in that case, and I think it handles such case better, since it makes no sense to have final newline without content of the file.

By the way, vim works this way too: it doesn't create a final newline with no content. You can check its behavior this way:

  1. Open empty file in Zed and at the same time in vim
  2. Save empty file in vim
  3. Switch back to Zed and you will see there is no final newline
  4. Switch back to Vim and type something
  5. Switch back to Zed and you will see final newline
  6. Switch back to Vim and remove everything, keep only two empty lines (as on your screenshot)
  7. Switch back to Zed and you will see 3 lines in total, since Vim adds final newline by default

joshuaia avatar Apr 26 '24 16:04 joshuaia

If you change the ensure_final_newline_on_save setting to false does that give you the behavior you want?

maxdeviant avatar Apr 26 '24 17:04 maxdeviant

Yes, if I'm saving empty file.

Here is how it works now:

https://github.com/zed-industries/zed/assets/51882760/96884f36-5e88-400e-9a83-98435159e6c4

What I'm expecting:

https://github.com/zed-industries/zed/assets/51882760/c87d29a5-36d1-4621-8548-551173bc4a02

joshuaia avatar Apr 26 '24 19:04 joshuaia

just add the following line to your settings if you want to disable it: "ensure_final_newline_on_save": false,

alexkotusenko avatar May 28 '24 20:05 alexkotusenko

I believe the OP is aware of the ensure_final_newline_on_save setting, and the point of this issue is to determine whether an extra terminating newline makes sense in the case of an empty file, which doesn't seem to me to be the case, since at least on most Linux systems only a single line is enforced.

Moshyfawn avatar May 28 '24 20:05 Moshyfawn