EDIT I didn't know the built-in features suggested by @Romainl in his answer: of course the -d
and +d
method should be prefered as it is way simpler than adding functions to your .vimrc
Maybe adding something like that to your .vimrc
could be what you want:
function! DeleteOver()
let save_cursor = getpos(".")
normal k
normal dd
call setpos(".", save_cursor)
normal k
endfunction
function! DeleteUnder()
let save_cursor = getpos(".")
normal j
normal dd
call setpos(".", save_cursor)
endfunction
These functions delete the line under or the line over the current one and go back to the position before the deletion.
For the function DeleteOver()
we need to go up one line with normal k
after the deletion because the deletion change the number of the original line which doesn't happend with DeleteUnder()
You can also add this to your .vimrc
nnoremap <Leader>d :call DeleteOver()<CR>
nnoremap <Leader>D :call DeleteUnder()<CR>
To create the mappings calling the functions.
nnoremap <Leader>d kddj
andnnoremap <Leader>D jddk
?kddj
orjddk
, column position is not preserved. I'm not saying it's a big problem, but it's annoying. For the record,kddj
moves cursor 1 line below original position. Andjdd-
seems like a better alternative tojddk
because at least I'm returning to first non white space character. If line below the one I want deleted is empty, I'm left at the beginning of the line.