Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move current line up or down in vim for vscode

Using VS Code, how can I move the current line I am on (or the current selection) up or down using Vim keybindings? When using the editor as normal, I can achieve this using 'alt + up/down'.

I am using the vscodevim extension.

like image 514
Hayden Avatar asked Feb 29 '20 09:02

Hayden


People also ask

How do you move a line up and down in VS Code?

If you put the cursor on a line of code and use the Alt+Up Arrow keys, the line of code you've selected moves up. If you use the Alt+Down Arrow keys, the line of code selected moves down.

How do you rearrange VS Code?

On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I move left and right in Vim?

To move left, press h . To move right, press l . To move down, press j . To move up, press k .


2 Answers

adding the following to the settings.json. It uses J and K to move the line down/up in the normal mode.

"vim.normalModeKeyBindingsNonRecursive": [
    {
        "before": ["J"],
        "commands": ["editor.action.moveLinesDownAction"]
    }, // moveLineDown
    {
        "before": ["K"],
        "commands": ["editor.action.moveLinesUpAction"]
    } // moveLineUp
],
like image 183
ahswch Avatar answered Sep 19 '22 13:09

ahswch


In vim there is no direct mapping for that, but what you can do is:

  • Place your cursor on the line you want to move.
  • Delete the line using: dd
  • Then go to the line right before where you want to place it.
  • Press p to past your deleted line.

That should do the trick.

like image 24
Sander Vanhove Avatar answered Sep 17 '22 13:09

Sander Vanhove