【发布时间】:2012-04-14 20:10:24
【问题描述】:
所以我发现在 Vim 中我的一个常见任务是 PUT 到行首或行尾。所以我的映射可能是:
nmap <Leader>p $p
nmap <Leader>P 0P
但是,我真正想做的是在放置之前选择性地包含一个寄存器。
例如 "a,P 将从寄存器 a 放到行首。
有没有办法通过映射做到这一点?
【问题讨论】:
标签: vim
所以我发现在 Vim 中我的一个常见任务是 PUT 到行首或行尾。所以我的映射可能是:
nmap <Leader>p $p
nmap <Leader>P 0P
但是,我真正想做的是在放置之前选择性地包含一个寄存器。
例如 "a,P 将从寄存器 a 放到行首。
有没有办法通过映射做到这一点?
【问题讨论】:
标签: vim
您可以在一行中使用<expr> 映射来做到这一点:
nnoremap <expr> \p '$"'.v:register.v:count1.'p'
nnoremap <expr> \P '0"'.v:register.v:count1.'P'
【讨论】:
这是完全可能的。我首先我虽然这个解决方案是可能的:https://stackoverflow.com/a/290723/15934,但是<expr> 不会让我们随意移动光标,并且normal 不能使用。
不过,我们可以这样做:
function! s:PutAt(where)
" <setline($+1> appends, but <setline(0> does not insert, hence the hack
" with getline to build a list of what should be at the start of the buffer.
let line = a:where ==1
\ ? [getreg(), getline(1)]
\ : getreg()
call setline(a:where, line)
endfunction
nnoremap <silent> <leader>P :call <sid>PutAt(1)<cr>
nnoremap <silent> <leader>p :call <sid>PutAt(line('$')+1)<cr>
【讨论】:
<expr> 映射确实允许您移动光标,您只需将光标移动命令添加到表达式的结果中。