How can I add emacs like control up/down to vscode?

Viewed 206

In emacs, if you hit control-up or control-down, you skip a group of code rather than a single line of code. Here is an example with control-down and | as the cursor

|
foo         foo
bar    ->   bar
baz         baz
            |

however, in vscode, if you do the same, the entire screen shifts up or down one line. How can I add the emacs like scrolling? It is the main way I like to move around on a screen. I haven't found any existing extensions which have this functionality

1 Answers

Update: new functionality added to Insiders' Build v1.54 makes this quite easy now. See https://stackoverflow.com/a/66066852/836330 For example (in keybindings.json):

    {
        "key": "ctrl+up",
        "command": "cursorMove",
        "args": {
            "to": "prevBlankLine",
            // "select": true
        },
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+down",
        "command": "cursorMove",
        "args": {
            "to": "nextBlankLine",
            // "select": true
        },
        "when": "editorTextFocus"
    },

Previous answer:

This is pretty close. You will need an extension to do it. Select By - it also moves by an amount that can be determined by a regex.

In your settings.json:


    "moveby.revealType": "InCenter",
  
    "selectby.regexes": {

      "goToEmptyLine": {
        "flags": "m",                // must use the "m" multiline flag for this regex
        "moveby": "^\\s*$\\r?\\n",   // an "empty" line, it may have spaces or tabs in it
      
        // "forward": "(^\\s*$\\r?\\n)",
        // "backward": "^\\s*$\\r?\\n"      
      }
    }

and in your keybindings.json:

    {
      "key": "ctrl+up",
      "when": "editorTextFocus",
      "command": "moveby.regex",
      "args": ["goToEmptyLine", "moveby", "prev", "start"]
    },

    {
      "key": "ctrl+down",
      "when": "editorTextFocus",
      "command": "moveby.regex",
      "args": ["goToEmptyLine", "moveby", "next", "start"]
    },

In the up direction it does skip consecutive empty lines, but not in the down direction - it stops right after text but with more empty lines below but it does skip them moving to the next down target.

That last point might be fixable with a different regex (you can set different regex's for forward and backward). But what is presented is very close to what you want.

Related