我的核心问题是如何创建允许计数和运动的自定义映射.但我希望伯爵能够推翻议案.为了澄清我想要以下工作:
[count][cmd] - 在[count]行上做一些有用的事情,而不是等待[动作].
[cmd][motion] - 在线条的[运动]范围内做一些有用的事情.
我的确切方案是尝试向行添加注释,但我会将此信息用于我的vimrc中的其他映射.这是我到目前为止所拥有的.
"comment motion of lines
nmap <silent> ,c :set opfunc=Comment<CR>g@
"comment count lines
nmap <silent> ,cc :s/^/\/\//<CR>:noh<CR>
function! Comment(...)
silent exe "'[,']s/^/\\/\\//"
silent exe "noh"
endfunction
Run Code Online (Sandbox Code Playgroud)
,c[motion]按动议评论.[count],cc按计数评论一行.
我想,c[motion]和[count],c工作.
这可能吗?
编辑:澄清了我的问题.将"范围"更改为"计数"
function s:ExecuteCountOrMotion()
setlocal operatorfunc=Comment
if v:count is 0
return 'g@'
else
return 'g@g@'
endif
endfunction
nnoremap <expr> ,c <SID>ExecuteCountOrMotion()
Run Code Online (Sandbox Code Playgroud)
顺便说一下,你不需要执行:
silent exe "{range}s/.../.../"你应该写{range}s/.../.../e.s#^#//#e以避免转义.silent nohl同样有效silent execute "nohl",但导致vim不解析额外的行.