Mapping a new motion in Vim with a required parameter -



Mapping a new motion in Vim with a required parameter -

i have been looking map new "motion" in vim takes parameter.

for example, know ciw "cut within word" , set insert mode, looking having custom action replace c (for illustration s) takes movements iw requires parameter.

a trivial illustration be:

given line in text file

and execute in normal mode (given cursor on first column) siw* surround first word * so:

*given* line in text file

i know, first-class surround.vim plugin does. giving illustration here, , looking reply how mappings above work.

i tried playing onoremap , opfunc can't seem them play way want.

so combination of motions plus operator pending mappings.

here illustration implementation of command described in question, illustrative purposes.

nnoremap <silent> s :set opfunc=surround<cr>g@ vnoremap <silent> s :<c-u>call surround(visualmode(), 1)<cr> function! surround(vt, ...) allow s = inputchar() if s =~ "\<esc>" || s =~ "\<c-c>" homecoming endif allow [sl, sc] = getpos(a:0 ? "'<" : "'[")[1:2] allow [el, ec] = getpos(a:0 ? "'>" : "']")[1:2] if a:vt == 'line' || a:vt == 'v' phone call append(el, s) phone call append(sl-1, s) elseif a:vt == 'block' || a:vt == "\<c-v>" exe sl.','.el 's/\%'.sc.'c\|\%'.ec.'c.\zs/\=s/g|norm!``' else exe el 's/\%'.ec.'c.\zs/\=s/|norm!``' exe sl 's/\%'.sc.'c/\=s/|norm!``' endif endfunction

to user input, function inputchar() used, assuming required argument single character.

function! inputchar() allow c = getchar() homecoming type(c) == type(0) ? nr2char(c) : c endfunction

if necessary take string argument, phone call input() instead of inputchar().

vim

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -