Display has an unintended effect

I created a mapping to move the current line up one line.

:map _ ddkP

It works, except that when executed on the first line, it deletes the first line.

I would expect that nothing will happen, because they are carried out dd, k, Pin the first row, nothing happens. Where am I mistaken?

+3
source share
2 answers

When you perform dd, k, P, commands are executed separately:

  • current row is deleted
  • the cursor moves one line
  • a previously inverted line is inserted above the current line

, . Vim , .

, .

dd , k , : Vim P.

" " - , .

- , , .

nnoremap _ :move-2<CR>

/.

Vim.

+8

map, , <expr> , , .

, vimrc :

function! Swap()
    if line('.') > 1 
        return 'ddkP'
    endif
    return ''
endfunction

map:

:map <expr> _ Swap()

, , if.

+1

All Articles