4

Running VS Code with the VSCodeVim emulation. Line wrapping and relative numbering is enabled in the configuration:

"editor.wordWrap": "on",
"editor.lineNumbers":"relative"

Display lines aren't numbered in the editor when a line is wrapped. Is it possible to configure VS Code to allow direct navigation to a display line?

12

VSCodeVim can navigate by display lines just like Vim, with gk and gj. You can remap j and k like this:

"vim.normalModeKeyBindingsNonRecursive": [
    {
        "before": ["j"],
        "after": ["g", "j"]
    },
    {
        "before": ["k"],
        "after": ["g", "k"]
    }
]
3
  • I have this setting in my config and it doesn't solve my (perceived) issue I'm afraid, whereas the same keybindings in my .vimrc do
    – RJones
    Mar 28 '18 at 9:36
  • Ah, interesting. I may not understand the perceived issue, then. Can you tell me more? Fair warning: I know more about Vim than VS Code. Mar 30 '18 at 19:11
  • It looks like the vim difference is coming straight from this vim.fandom.com/wiki/Move_cursor_by_display_lines_when_wrapping. Regular vim has the same issue, it's just vscode's way of saying "map j to gj).
    – Schneems
    Sep 10 at 16:46
7

If you have word wrap on and would like the cursor to enter each wrapped line when using j, k, or , set the following in VS Code's keybindings.json settings file (as in Andrew's answer, other options exist but they are slow):

{
  "key": "up",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "down",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "k",
  "command": "cursorUp",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
},
{
  "key": "j",
  "command": "cursorDown",
  "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
}

Ref: The VSCodeVim Readme (after my documentation PR)

3
  • Thank you so much. Is there a way to make this also work with other modes that Normal? When I removed vim.mode == 'Normal' check and tried to visually select a block the vim cursor wont follow anymore. Also this seems to break commands like "10j" or "2k" etc. May 9 '20 at 10:40
  • Yeah, this breaks chords :( I don't know any more about using it in different modes...
    – Karl Horky
    May 13 '20 at 13:49
  • More important than chords, this breaks moving after having enabled selection move (capital V), and have this moving around expand the selection. It's a pity because otherwise it works fantastically (and I do not use those kinds of chords anyway).
    – Ernesto
    Apr 5 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.