Search code examples
vimvim-pluginneovimvscodevim

Is there any way to enclose a variable with print() statement or any function in vim especially using vim vs code extension


suppose I have a variable named

a_variable

is there a surround or some combination of keystrokes which will do

print(a_variable)

or

print("a_variable: ", a_variable)

when my cursor in on the line where a_variable is defined ? I could not find any relevant material on the web . Any help is appreciated.


Solution

  • This answers is written with Vim in mind. Some, all, or none of it may apply to your specific Vim emulator so YMMV.

    With the surround plugin

    First case

    You can position your cursor on a_variable and do:

    ysiwfprint(<CR>
    

    to obtain:

    print(a_variable)
    

    Second case

    It is currently impossible to achieve with surround only.

    Without surround

    First case

    You can position your cursor on a_variable and do:

    ciwprint(<C-r>")<Esc>
    

    Second case

    The scond case is a variant of the first case where you insert the variable name two times instead of one:

    ciwprint("<C-r>": ", <C-r>")<Esc>
    

    Turn it into a mapping if you need to do it often.

    See :help c, :help iw, :help i_ctrl-r, :help "".