How to center the editor window back on the cursor in VSCode?
Asked Answered
P

4

37

I'm using VSCode as my text editor. I'm curious, is there a keybinding for centering the editor window on the cursor, when the window is a lot of lines below/above it such that it's not visible on the screen? I've tried looking at the default keybindings by going to FIle > Preferences > Keyboard Shortcuts, but I see no such options for centering the window.

Panada answered 11/12, 2016 at 3:30 Comment(0)
P
55

There is no such keybinding / command built-in.

I couldn't stand that either, so I created a VSCode extension. You can find it and install it here on the marketplace. The default shortcut is CTRL + L.

Potful answered 12/12, 2016 at 6:43 Comment(6)
is there such a plugin such that the screen is always centered on the cursor? I use vim as my primary text editor and I'm hoping VScode has similar functionality to this – Junker
Not that I am aware of. But sounds like an interesting extension to build. πŸ€” – Potful
In that case it might be the first extension I build, haha – Junker
This is great! Why don't you implement it in the editor itself. To me, this looks like something that the editor by default show can benefit from. Just a thought. – Bedaub
@DanielSantos unfortunately VSCode is full of illogical choices for default settings, or in this case lack of them. – Brentwood
@PatrickMichaelsen have you made the extension, I'm trying to find an extension that will always center the cursor on the screen. – Grammalogue
S
28

If you want to always keep the cursor on the center, you can change the setting editor.cursorSurroundingLines to a really high number (100 is ok) and that should work.

Stagner answered 15/1, 2021 at 21:24 Comment(2)
Great one! So helpful, why VS Code use 0 as surrounding lines ? – Henryk
related: https://mcmap.net/q/425836/-maintain-screen-position-always-centered-on-cursor/11107541 – Epiblast
E
21

If you are using vscodevim, zz should work.

Exodus answered 15/10, 2019 at 11:11 Comment(0)
B
3

As @kwood put it, there is an extension Center Editor Window in the marketplace that meets this end.

I would like to make an answer here, to complement that -- even the extension's author did not explicitly state in the marketplace page -- if you would like to change the default key binding (Ctrl+L), you may try put the following lines in the keybindings.json for the keyboard bindings.

{
    "key": "cmd+k cmd+c",
    "command": "center-editor-window.center",
    "when": "editorTextFocus"
},

The above command sets ⌘ Command+K, ⌘ Command+C as the keyboard shortcut.


Apart from that, you may set

"center-editor-window.threeStateToggle": true,

in the settings.json for VS Code settings so that it will switch among three states (center, top, bottom) instead of one (center).

Brachio answered 6/3, 2020 at 6:19 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.