0

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
Sanket Wagh
  • 156
  • 1
  • 14

1 Answers1

2

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 "".

romainl
  • 186,200
  • 21
  • 280
  • 313
  • in the `ysiwfprint(` what does `f` do here ? – Sanket Wagh Nov 17 '21 at 04:28
  • 1
    The basic use case for that surround plugin is adding/changing/removing surrounding pairs like `()`, etc. The `f` is one added functionality that is [described](https://github.com/tpope/vim-surround/blob/master/doc/surround.txt#L138) in surround's documentation. – romainl Nov 17 '21 at 06:29
  • thanks, it cleared my doubt regarding `f` still have no luck doing this operation in vscode vim. – Sanket Wagh Nov 17 '21 at 08:22
  • As noted, everything with `ysiw` above depends on a Vim plugin that definitely works in Vim once you have installed it. I know some Vim *emulators* include a re-implementation of surround… maybe your specific Vim *emulator* does, maybe it doesn't, maybe it does but it is incomplete, etc. I have no idea. Everything with `ciw` above is vanilla Vim and is guaranteed to work even in the smallest Vim builds, without any plugin. But again, there is no guarantee that your Vim *emulator* supports any of that. – romainl Nov 17 '21 at 09:00
  • `ysiw` works fine but `f` puts be back in normal mode. :(. Thanks for the help though. I have just started learning vim so started learning with vscode extension. – Sanket Wagh Nov 17 '21 at 09:07
  • I'd recommend learning $THING with $THING. – romainl Nov 17 '21 at 09:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239303/discussion-between-sanket-wagh-and-romainl). – Sanket Wagh Nov 17 '21 at 09:24