How can I quickly add something to multiple lines in vim?

I'm trying to get away from using the arrow, but there is one thing that I haven't decided yet without using the arrow keys. Take this example:

var1 = "1"
var2 = "2"
var3 = "3"
var4 = "4"

Now I want this to be:

var_1 = "1"
var_2 = "2"
var_3 = "3"
var_4 = "4"

Using the arrows, I would just get var1, insert and add an underline, and then the down arrow and do the same. The problem with using hjkl is that I cannot be in insert mode, so I need to release, move down, paste ... repeat the repeat, which required more work. Is there any other way to do this?

+3
source share
5 answers

You can also use the visual block insert:

  • go to "1" in "var1"
  • click CTRL+V
  • j ,
  • I ( i)
  • _
  • <ESC>

( - )

+7

. , :

  • 1G0
  • f1 → "1"
  • i_<ESC> → "_"
  • j.
  • j.
  • ...

, , "ex":

  • :%s/var/var_/

, johusman.

+4

, 1, 1...

:

qqfra_<Esc>+q3@q
  • q
  • q q
  • f
  • r 'r'
  • a
  • _
  • Esc
  • +
  • 3
  • @
  • q q

11.

() substitute:

:%s!r!&_<CR>

Par 9!

[... VimGolf!]

+1

I prefer: replace with visual block mode.

%s/var\zs\ze\d/_/
0
source

I always have line numbers, so I would do for example

1,4 s/var/var_/

It looks like

% s/var/var_/

but it only works on named strings. You can use visual mode to mark lines if you don't like to enter a range (prefix 1,4) in your command.

0
source

Source: https://habr.com/ru/post/1793709/


All Articles