vimsurround

vim - surround text with function call


I want to wrap some code :

myObj.text;

with a function call where the code is passed as an argument.

console.log(myObj.text);

I've thought about using surround.vim to do that but didn't manage to do it.

Any idea if it's possible ? I


Solution

  • With Surround in normal mode:

    ysiwfconsole.log<CR>
    

    With Surround in visual mode:

    Sfconsole.log<CR>
    

    Without Surround in normal mode:

    ciwconsole.log(<C-r>")<Esc>
    

    Without Surround in visual mode:

    cconsole.log(<C-r>")<Esc>
    

    But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:

    xnoremap <key> cconsole.log(<C-r>")<Esc>
    nnoremap <key> ciwconsole.log(<C-r>")<Esc>
    

    which brings us back to Surround, which already does that—and more—very elegantly.