0

Pretty new to mappings in Vim,

In Diff Mode I've got C-Up and C-Down mapped to [c and ]c for diffjumping. I'm wanting to map C-Right and C-Left to :<C-U>diffput v:count<CR> and :<C-U>diffget v:count<CR> respectively to speed up merging.

nnoremap <C-Left> :<C-U>diffget v:count<CR>
nnoremap <C-Right> :<C-U>diffput v:count<CR>
nnoremap <C-Up> [c
nnoremap <C-Down> ]c

This should.... put from buffer # or get from buffer #.

But I get the error "no matching buffer for v:count".

the command looks to be taking v:count literally and running :diffget v:count instead of :diffget 3 for example.. how do I get v:count to resolve to a number? like dereference the variable?

I can echo "diffget v:count" and that looks to be correct... but obviously doesn't do anything...

6
  • 1
    The right-hand side is the literal text you would type; as you already figured our it's not evaluated. To evaluate VimScript in there you can use an expression mapping by adding <expr>: nnoremap <expr> <C-Left> ":\<C-U>diffget " .. v:count .. "\<CR>". Didn't test that so may not work exactly, but should point you in the right direction. Note that because it's a VimScript expression, you need to put literal text in strings. Special <..> only work in double-quoted strings as "\<..>". Apr 1, 2022 at 12:19
  • @MartinTournoij Worked Perfectly! Thanks - learning slowly! :) don't know how to add mark a comment as the answer..
    – Jack T
    Apr 1, 2022 at 12:28
  • What is the purpose of the " .. " if you don't mind explaining? Looks like concat for strings?
    – Jack T
    Apr 1, 2022 at 12:29
  • Well, you're not really supposed to answer questions in comments, so you can't accept it 😅 As for the .., that's indeed just string concatenation. Actually just . also works, but .. is the "new" way of doing it (it solves some ambiguity in some cases with . for other uses like decimals in numbers). Apr 1, 2022 at 12:32
  • Thanks @MartinTournoij you're so helpful.
    – Jack T
    Apr 1, 2022 at 13:06

1 Answer 1

1

Instead of using <expr> as suggested in the comments, I would first try

nnoremap <C-Left> :<C-u>execute 'diffget' v:count<CR>

as in How do I use a variable or return value in option, command, or mapping?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.