在vim中使用v:count映射 [英] Mapping with v:count in vim
问题描述
我想将< c-c> 映射到副本,并在众所周知的< c-c> 上进行了一些改进捷径.如果可能的话,我也想为其使用通用映射.我听说过 v:count ,我想知道a是否可以在这里使用它.
在光标下复制单词inoremap< silent>< c-c>< esc> m`viw"+ y''annoremap< silent>< c-c>m`viw"+ y"复制副本vnoremap< c-c>"+ y在寄存器[count]中的光标下复制单词inoremap< silent>1 c-c< esc> m`viw"1y''ainoremap< silent>2 c-c< esc> m`viw"2y''ainoremap< silent>3< c-c>< esc> m`viw"3y''a[...]inoremap< silent>7 c-c< esc> m`viw"7y''ainoremap< silent>8< c-c>< esc> m`viw"8y''ainoremap< silent>9 c-c< esc> m`viw"9y''annoremap< silent>1 c-cm`viw"1y``nnoremap< silent>2 c-cm`viw"2y``nnoremap< silent>3< c-c>m`viw"3y``[...]nnoremap< silent>8< c-c>m`viw"8y``nnoremap< silent>9 c-cm`viw"9y``
问题是,我可以使用这种东西吗?我该怎么做?
nnoremap< silent>< c-c>m`viw"{v:count} y``
在您的帮助下,我做到了,但仍然有一些问题.例如,当我执行3时,它将粘贴"e"寄存器的内容3次.如何避免呢?
nnoremap< expr>< C-c>MyYank()inoremap< expr>< C-c>MyYank()vnoremap< expr>< C-c>MyYank()nnoremap< expr>< C-v>MyPaste('n')不使用带编号的寄存器,因为它们由于quote_number而旋转".而是使用索引字符串将< count>映射到字母让s:mapping ='qwertzuiop'fu!MyYank(...)获取登记册以拉入令l:count = v:count>len(s:mapping)?0:v:计数让l:regs = l:count?s:mapping [l:count-1]:'+'动作取决于当前模式令l:currentmode = a:0>0?a:1:mode()如果l:currentmode =='n'返回'm`viw''.l:regs.'y''elseif l:currentmode =='i'返回"\ e".'m`viw''.l:regs.'y''a'elseif l:currentmode =='v'返回''.l:regs'y'万一Endfufu!MyPaste(...)获取登记册以拉入令l:count = v:count>len(l:mapping)吗?0:v:计数让l:regs = l:count?l:mapping [l:count-1]:'+'动作取决于当前模式令l:currentmode = a:0>0?a:1:mode()如果l:currentmode =='n'返回''.l:regs.'P'elseif l:currentmode =='i'返回"\ e".'m`viw''.l:regs.'y''a'elseif l:currentmode =='v'返回''.l:regs'y'万一Endfu
您可以使用:help:map-expr
:
在寄存器[count]中的光标下复制单词nnoremap< expr>< silent>< c-c>'m`viw'(.v:count?v:count:'"').``y''
但是,像@FDinoff一样,我会要求您重新考虑拉动编号寄存器的方法.如果您修改原始的< C-c>
映射(使用:map-expr
和 v:register
),则已经可以进入任意寄存器(以"{reg}
为前缀)仍然默认为剪贴板.那些带有计数的:imaps
会在输入数字时引起明显的延迟.>
I would like to map <c-c> to copy with some improvements over the well known <c-c> shortcuts. If possible I would also like to use a generic mapping for it. I heard about v:count and I am wondering if a can use it here.
" Copy word under cursor
inoremap <silent> <c-c> <esc>m`viw"+y``a
nnoremap <silent> <c-c> m`viw"+y``
" Copy selection
vnoremap <c-c> "+y
" Copy word under cursor in register [count]
inoremap <silent> 1<c-c> <esc>m`viw"1y``a
inoremap <silent> 2<c-c> <esc>m`viw"2y``a
inoremap <silent> 3<c-c> <esc>m`viw"3y``a
[...]
inoremap <silent> 7<c-c> <esc>m`viw"7y``a
inoremap <silent> 8<c-c> <esc>m`viw"8y``a
inoremap <silent> 9<c-c> <esc>m`viw"9y``a
nnoremap <silent> 1<c-c> m`viw"1y``
nnoremap <silent> 2<c-c> m`viw"2y``
nnoremap <silent> 3<c-c> m`viw"3y``
[...]
nnoremap <silent> 8<c-c> m`viw"8y``
nnoremap <silent> 9<c-c> m`viw"9y``
The question is, can I use something like this and how can I do it ?
nnoremap <silent> <c-c> m`viw"{v:count}y``
Edit:
With your help, I made this but I still have some issues with it. For instance, when I do 3 it will paste the content of the 'e' register 3 times. How to avoid that ?
nnoremap <expr> <C-c> MyYank()
inoremap <expr> <C-c> MyYank()
vnoremap <expr> <C-c> MyYank()
nnoremap <expr> <C-v> MyPaste('n')
" Not using numbered registers because they get rotated due to quote_number
" Instead. A indexed string is used to map <count> to a letter
let s:mapping = 'qwertzuiop'
fu! MyYank(...)
" Get the register to yank in
let l:count = v:count > len(s:mapping) ? 0 : v:count
let l:regs = l:count ? s:mapping[l:count - 1] : '+'
" Action depends on the current mode
let l:currentmode = a:0 > 0 ? a:1 : mode()
if l:currentmode == 'n'
return 'm`viw"' . l:regs . 'y``'
elseif l:currentmode == 'i'
return "\e" . 'm`viw"' . l:regs . 'y``a'
elseif l:currentmode == 'v'
return '"' . l:regs . 'y'
endif
endfu
fu! MyPaste(...)
" Get the register to yank in
let l:count = v:count > len(l:mapping) ? 0 : v:count
let l:regs = l:count ? l:mapping[l:count - 1] : '+'
" Action depends on the current mode
let l:currentmode = a:0 > 0 ? a:1 : mode()
if l:currentmode == 'n'
return '"' . l:regs . 'P'
elseif l:currentmode == 'i'
return "\e" . 'm`viw"' . l:regs . 'y``a'
elseif l:currentmode == 'v'
return '"' . l:regs . 'y'
endif
endfu
You can do that with a :help :map-expr
:
" Copy word under cursor in register [count]
nnoremap <expr> <silent> <c-c> 'm`viw"' . (v:count ? v:count : '"') . 'y``'
However, like @FDinoff, I'd ask you to reconsider your approach of yanking to the numbered registers. If you rework your original <C-c>
mapping (using :map-expr
and v:register
), you can already yank into arbitrary registers (by prefixing with "{reg}
) and still default to the clipboard. And those :imaps
with the count will cause a noticeable delay when entering numbers.
这篇关于在vim中使用v:count映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!