How can I prevent VS Code from jumping to the bottom of a file after formatting?
Asked Answered
T

3

10

How can I prevent VS Code from jumping to the bottom of a file after formatting?

I select the whole file content (using the Ctrl + A), then I format everything (using the Alt + Shift + F). After that I would expect my file to be formatted and the view not to move. The file gets formatted, but the view jumps to the bottom.

How could I prevent this jumping?

enter image description here

Tollefson answered 12/1, 2022 at 12:24 Comment(0)
C
5

When you are doing Ctrl+A your cursor is implicitly moved to the end of the selection, hence the end of the file. Therefore, your cursor is not moved at the end by the formatting but by the selection.

The solution is to format the whole file (with Alt+Shift+F) without selecting the content before (Ctrl + A).

Moreover, in your case, Ctrl+A was useless because you are using the shortcut for formatting the entire document. If you only want to format a selection, the shortcut is Ctrl+K Ctrl+F.

Castor answered 12/1, 2022 at 13:5 Comment(2)
Oh. The magic has never been easier. Thank you. :)Tollefson
Yeah, simple answer. Though, when the file is extremely long, Alt+Shift+F may still go to end of file, happened to me before.Sheeree
P
6

I was facing similar issue on saving file even without selecting all content.

Format on save was ON using Prettier formatter.

Problem was the file size, when it is huge say more than 100KB this issue started.

After breaking the file content into different files and removing unnecessary content in the files to reduce the size, issue resolved.

Provo answered 12/3, 2023 at 7:27 Comment(3)
I have the same issue, but this doesn't really answer the question. why is prettier behaving this way, and how to prevent this?Midshipmite
@MARC, question was not on why Prettier is behaving like this, it was how to prevent after Formatting, what I suggested is to Prevent it by reducing the file size by moving some of the content to another file and import it.. I dont have answer to why Prettier is doing that.Provo
@Midshipmite This bug was likely introduced in plugin version 9.5. Reverting to 9.4 works for me. More details: github.com/prettier/prettier-vscode/issues/…Canara
C
5

When you are doing Ctrl+A your cursor is implicitly moved to the end of the selection, hence the end of the file. Therefore, your cursor is not moved at the end by the formatting but by the selection.

The solution is to format the whole file (with Alt+Shift+F) without selecting the content before (Ctrl + A).

Moreover, in your case, Ctrl+A was useless because you are using the shortcut for formatting the entire document. If you only want to format a selection, the shortcut is Ctrl+K Ctrl+F.

Castor answered 12/1, 2022 at 13:5 Comment(2)
Oh. The magic has never been easier. Thank you. :)Tollefson
Yeah, simple answer. Though, when the file is extremely long, Alt+Shift+F may still go to end of file, happened to me before.Sheeree
M
3

This could (currently, or in the past) be due to a bug in VS Code related to its extension API, or in an extension using that API to implement formatting support.

For an example of a VS Code bug, see https://github.com/microsoft/vscode/issues/71707.

If you have the Prettier extension installed, know that it's had issues with this in the past, and continues to have them- especially for very large files:

If you're getting an issue like this, you can do an extension bisect to find out if it's a specific extension that's cause this, or just check your settings to see what you've configured the default formatter to be for the language you're seeing this problem in, and then check the issue tracker for that extension (you can find the link to its issue tracker in its page in the Extensions View or in its marketplace page) to see if the issue has already been reported, and if not, raise a new issue ticket to report the issue (in the appropriate repo), providing as much detail about how to minimally reproduce the issue. You can also try rolling back to previous versions of the problematic extension to see if the problem doesn't happen in previous versions.

Mousy answered 18/12, 2023 at 5:37 Comment(1)
setting endOfLine to "auto" worked for me in large HTML filesCrumble

© 2022 - 2024 — McMap. All rights reserved.