chriserwin
chriserwin

Reputation: 1242

How do you prevent VS Code from reopening file at previous scroll position

When using VS Code I'm experiencing unexpected behaviour. When I re-open a file, the editor opens at the previous scroll position.

Example:

screencast

I expected that the editor would reopen the file back at the top of the file like in other text editors.

I haven't been able to find any settings that would explain this behavior.

Upvotes: 6

Views: 1190

Answers (3)

VonC
VonC

Reputation: 1323095

unexpected behaviour

It is actually very much the expected behavior: restore the workbench in its exact state.

This is confirmed by issue 24394, which is asking (indirectly) what you want.

This behavior should be opt-in and disabled by default.
I often open a file, scroll to the bottom looking for something and close it then.

Now nearly every file I open starts at the bottom instead of the top and in 90% of the times the first thing I do is scroll to the top.

Commit 27cb886 from Sept. 2018, VSCode 1.28, introduces the setting

'workbench.editor.restoreViewState': {
        'type': 'boolean',
        'description': nls.localize('restoreViewState', 
         "Restores the last view state (e.g. scroll position) when re-opening files 
          after they have been closed."),
        'default': true,
},

That fixes the issue.


Update August 2020, VSCode 1.49

Allow to configure workbench.editor.restoreViewState per language

When we close a file and then re-open it, VS Code will restore the cursor position.

In many cases this is desired.
However, in some cases it is not, therefore it would be useful to provide a setting for this and allow it to be configured differently depending on the language being used.

For example: when I run git commit in terminal, the COMMIT_EDITMSG file will open in VS Code.
Ideally my cursor would be positioned at the top so I can start writing my commit message right away. However, many times my cursor is positioned elsewhere, presumably because VS Code is trying to restore it to the position when I closed this file the last time I wrote a commit message.

This also happens when I run git rebase --interactive and the rebase todo list is opened in VS Code.

This is now fixed:

  • configure workbench.editor.restoreViewState for a language, e.g. markdown
  • open a couple of files including markdown and scroll + set a selection you can memorize
  • switch between tabs and verify view state is restored
  • reload window and do the same and verify view state is restored
  • close a non-markdown file and open it again and verify view state is restored
  • close the markdown file and open it again and verify view state is NOT restored

Upvotes: 2

DJCONTEL
DJCONTEL

Reputation: 41

just add the following line in the settings.json:

"workbench.editor.restoreViewState": false

Upvotes: 4

Natsfan
Natsfan

Reputation: 4802

If you specify the filename when you run VScode. the file is shown at the beginning. For example, I have a file named 2gaussians.py. When I first started on this file I entered code at the command prompt on the command line. I opened my file from within VScode. I worked on my file and I quit with my cursor at the bottom of the file. When I ran VScode the next time I typed code 2gaussians.py on the command line. VScode opened and my file, 2gaussians.py, was displayed in the editor from line 1.

Upvotes: 1

Related Questions