loki
loki

Reputation: 2311

VS Code scroll up/down moving the cursor

When scrolling down in VSCode. Using arrow keys makes you have the cursor on the bottom. I can use Ctrl + Arrow Down This scrolls the screen and the cursor keeps its position. In this case I need to click on the new line in order to start editing. However I'm looking for a way to scroll and move the cursor. For example if I'm in the middle of the screen I want to scroll and get the cursor to maintain its relative position in the middle.

Has anyone done this?

Upvotes: 7

Views: 6147

Answers (4)

Paul Rademacher
Paul Rademacher

Reputation: 29

Custom keyboard shortcuts using runCommands to chain together the scroll and the cursor move. No extensions needed anymore!

{
    "command": "runCommands",
    "key": "ctrl+shift+up",
    "args": {
        "commands": [
            "scrollLineUp",
            "cursorUp"
        ]
    }
},
{
    "command": "runCommands",
    "key": "ctrl+shift+down",
    "args": {
        "commands": [
            "scrollLineDown",
            "cursorDown"
        ]
    }
}

Upvotes: 1

Ciemrnt
Ciemrnt

Reputation: 1

just in case someone also search this, my workaround was to add this shortcut to VSCode

    {
        "key": "ctrl+shift+l",
        "command": "cursorMove",
        "when": "editorTextFocus",
        "args": {
            "to": "viewPortCenter",
            "by": "line"
        }
    }

This place the cursor in the middle of the screen, so now you can scroll with ctrl+up/down and place your cursor in the middle of the screen with ctrl+shift+l.

Also with the extension scroll to cursor you can go back to your cursor by doing ctrl+L.

Hope this can help someone !

Upvotes: 0

Dziad Borowy
Dziad Borowy

Reputation: 12579

It's now possible with these commands:

    {
        "key": "cmd+down",
        "command": "editorScroll",
        "when": "textInputFocus",
        "args": {
            "to": "down",
            "by": "line",
            "revealCursor": true,
            "value": 1
        }
    },
    {
        "key": "cmd+up",
        "command": "editorScroll",
        "when": "textInputFocus",
        "args": {
            "to": "up",
            "by": "line",
            "revealCursor": true,
            "value": 1
        }
    }

Upvotes: 0

rioV8
rioV8

Reputation: 28623

With the help of the extension Multi Command

Add this setting

  "multiCommand.commands": [
    {
      "command": "multiCommand.up1LineKeepCursor",
      "sequence": [
        {"command": "editorScroll", "args": {"to": "up", "by": "line" }},
        "cursorUp"
      ]
    },
    {
      "command": "multiCommand.down1LineKeepCursor",
      "sequence": [
        {"command": "editorScroll", "args": {"to": "down", "by": "line" }},
        "cursorDown"
      ]
    }
  ]

And these keybindings

  {
    "key": "shift+ctrl+alt+up",
    "command": "multiCommand.up1LineKeepCursor",
    "when": "editorTextFocus"
  },
  {
    "key": "shift+ctrl+alt+down",
    "command": "multiCommand.down1LineKeepCursor",
    "when": "editorTextFocus"
  }

You can use any key binding you like.

It works good when Word Wrap is OFF.

Upvotes: 8

Related Questions