t3o
t3o

Reputation: 329

How can I define a key mapping in vim and use the repeat number more than once?

I edit files with relative line numbers. Often I would like to copy a line from let's say 16 lines above to the current location.

In normal mode I would enter: 16kyy16jP

But when it is line 14, it is: 14kyy14jP

How can I define a key mapping/command to be able to enter something like 16LK or 14LK in normal mode to achieve the same result?

Upvotes: 1

Views: 298

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32926

May be something like

nnoremap <silent> µ :<c-u>exe "normal! ".v:count1."kyy".v:count1."jP"<cr>

But, honestly, I'd use functions here as there is no need to move around that much:

nnoremap <silent> µ :<c-u>call append(line('.')-1, getline(line('.')-v:count1))<cr>

Note that the following also works thanks to :yank

nnoremap <silent> µ :<c-u>exe '-'.v:count1.'y'<cr>P

EDIT: I didn't know about :t, @romainl's answer (with @Kent's patch) makes more sense than mine. If you want a mapping it could be mode with:

nnoremap <silent> µ :<c-u>exe '-'.v:count1.'t-1'<cr>
" which isn't much different than the previous answer.

Upvotes: 3

romainl
romainl

Reputation: 196496

16kyy16jP

What a waste… You could use :help :t instead:

:-16t.
:-14t.

Upvotes: 5

Kent
Kent

Reputation: 195039

You can map a function call, which accepts input parameters.

function! YourMap(n) 
    exec 'normal! '.a:n.'-Y'.a:n.'+P'
endfunction
nnoremap <leader>c :call YourMap(input('lines:')) <CR>

You press <leader>c, then input the relative line numbers, the copy/paste should be done.

The <leader>c is the mapping key, you can change it into other key combinations.

Upvotes: 1

Related Questions