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.
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.
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
],
I have found adding the following to the keybindings.json
works. It remaps the keys back to the native Visual Studio Code move lines commands. In this example I am using Command-Alt-Up/Down, but in theory you could use change the mappings to just Alt-Up/Down as you asked.
{
"key": "cmd+alt+up",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadOnly"
},
{
"key": "cmd+alt+down",
"command": "editor.action.moveLinesDownAction",
"when": "editorTextFocus && !editorReadOnly"
},
I will add that when there are multiple lines selected in visual mode it seems to select below it when moving up and I am not sure why. escape
or just moving the cursor clears that extra selection.
In vim there is no direct mapping for that, but what you can do is:
dd
p
to past your deleted line.That should do the trick.
I think that this is what you want
add this to your .vimrc
" Move lines up and down
nnoremap <C-Down> :m .+1<CR>==
nnoremap <C-Up> :m .-2<CR>==
inoremap <C-Down> <Esc>:m .+1<CR>==gi
inoremap <C-Up> <Esc>:m .-2<CR>==gi
vnoremap <C-Down> :m '>+1<CR>gv=gv
vnoremap <C-Up> :m '<-2<CR>gv=gv
source : https://vim.fandom.com/wiki/Moving_lines_up_or_down
Works for me after adding this in setting.json
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["ctrl+j"],
"commands": ["editor.action.moveLinesDownAction"]
},
{
"before": ["ctrl+k"],
"commands": ["editor.action.moveLinesUpAction"]
}
]
© 2022 - 2024 — McMap. All rights reserved.
Control
key instead ofShift
use"<C-j>"
"<C-k>"
instead of"J"
,"K"
For me that is default vim behavior. – Semester