I’m trying out vscode for editing, since it has a nice “file explorer” capability that I don’t get (at least by default) with GVim. However, the scrolling behaviour annoyed me. But I found a solution!

The Issue

I typically use the cursor keys to scroll in my editor. Often, I want the cursor to stay on the same line of code while the window scrolls. This allows me to see more context above or below the location I’m editing without losing my place (cursor position). In many editors, you get this behaviour by holding down the Ctrl key while pressing the cursor key. Even when I don’t explicitly care about keeping my cursor on the same line of code, holding Ctrl has the advantage of immediately scrolling the view, rather than having to wait until the cursor reaches to top/bottom line of the window. Consequently, I’m not in the habit of “Ctrl-scrolling” more often than not.

The question is: What happens when the line of code that contains the cursor disappears off the screen? Some editors will move the cursor to a different line of text in order to keep it on the screen. That’s the behaviour I like. However, some editors will keep the cursor on the same line of text, even if that line is no longer visible on screen. In this case, once you stop Ctrl-scrolling and switch back to typing new text, or scrolling without Ctrl pressed, the editor will snap the scroll position back so that the cursor is still visible. I find this behaviour really annoying, since I typically Ctrl-scroll just to find text I’m interested in, and actively want to cursor to move there rather than preserving the cursor position.

The Solution

VSCode’s keybindings can be customized to work the way I want:-) I found the solution in the following github issue comment:

https://github.com/microsoft/vscode/issues/2091

To apply that solution, select File menu -> Preferences -> Keyboard Shortcuts (or press Ctrl-K Ctrl-S). This will open the regular keyboard shortcuts editor. From there, to manually edit keybindings, look for a button in the top-right of the editor with tool-tip “Open Keyboard Shortcuts (JSON)” and click it. It looks like a tiny document with an arrow pointing into it. That will open a JSON file that you can edit. Paste in the following content and save it. You don’t even need to restart VSCode for the modification to take effect! For more information, see https://code.visualstudio.com/docs/getstarted/keybindings.

[
	{
		"key": "ctrl+up",
		"command": "editorScroll",
		"when": "editorTextFocus",
		"args": 
		{
			"to": "up",
			"by": "line",
			"revealCursor": true
		}
	},
	{
		"key": "ctrl+down",
		"command": "editorScroll",
		"when": "editorTextFocus",
		"args": 
		{
			"to": "down",
			"by": "line",
			"revealCursor": true
		}
	}
]