Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapping lines creates new empty lines #3602

Open
korikhin opened this issue Jan 7, 2025 · 4 comments
Open

Swapping lines creates new empty lines #3602

korikhin opened this issue Jan 7, 2025 · 4 comments

Comments

@korikhin
Copy link

korikhin commented Jan 7, 2025

If there is an empty line below the current line, swapping lines (using ⌥↓) creates new empty lines.

1 hello world  // cursor is here
2

Becomes (after pressing twice)

1
2
3 hello world  // cursor is here
4

Also:

1
2 hello world  // cursor is here

Press ⌥↑ and You get

1 hello world  // cursor is here
2
3 // new empty line created
@Andriamanitra
Copy link
Contributor

The current behavior makes sense if you think of the newline characters as being part of the line 1. If you think of the newline characters as separators between the lines 2 it appears a new line is created. I can't think of an objective reason to prefer either way. Is there some use case where you would not want the "added" trailing newline?

Footnotes

  1. "hello\nworld\n" => ["hello\n", "world\n"]

  2. "hello\nworld\n" => ["hello", "world", ""]

@hholst80
Copy link

hholst80 commented Jan 9, 2025

I think this is a bug. Moving line 2 up in a file with 2 lines should not end up with 3 lines no matter how you phrase it it is just not the expected result.

@Andriamanitra
Copy link
Contributor

I think this is a bug. Moving line 2 up in a file with 2 lines should not end up with 3 lines no matter how you phrase it it is just not the expected result.

And it doesn't if you think of the newline character as part of the line: len(["A\n", "B"]) == len(["B\n", "A\n"]) == 2. I guess it's still a little bit surprising that the "B" gets a newline appended to it, but perhaps less so than if ["A\n", "B"] became ["BA\n"] after a swap.

I understand that the current behavior is unexpected (to some) but I'm not sure if there's an use case where it's undesirable.

@korikhin
Copy link
Author

korikhin commented Jan 10, 2025

I get your point on considering \n as a part of the line, but we are humans, so it feels kinda counterintuitive. At least to my taste. This is the first time I see such a behaviour.

So if you want to have a new line at the very end of the file — just add it yourself after moving the text. That's why in (most) formatters having a trailing newline is an option.

As @hholst80 noticed, getting an "out of nowhere" new line is still an undesired thing in itself. I'd like to have control and don't want to be distracted by such a paper cut.

My point is that moving text should be bounded by the EOF by default and not push this boundary further. One could easily move some text lower than intended by accident and then deal with a bunch of empty lines after moving it back to the right place. This is the actual case. And furthermore, I use a buffer with history, so I don't always want to cut/paste since it bloats the history that I frequently search through (visually too).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants