Vim command with count and motion possible? -


my core question how create custom mapping allows count , motion. count override motion. clarify want following work:

[count][cmd] - useful on [count] lines, , not wait [motion].

[cmd][motion] - useful in range of [motion] of lines.

my exact scenario trying add comments lines, use information other mappings in vimrc. here have far.

"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 

,c[motion] comments line motion. [count],cc comments line count.

i ,c[motion] , [count],c work.

is possible?

edit: clarified quesiton. changed "range" "count"

function s:executecountormotion()     setlocal operatorfunc=comment     if v:count 0         return 'g@'     else         return 'g@g@'     endif endfunction nnoremap <expr> ,c <sid>executecountormotion() 

by way, don't need execute:

  1. instead of silent exe "{range}s/.../.../" should write {range}s/.../.../e.
  2. you can use s#^#//#e avoid escaping.
  3. silent nohl works silent execute "nohl", causes vim not parsing of additional line.

Comments