how to insert text at specific lines in vim?

Viewed 97

I want to put cursor at specific lines that is:

AAA
BBB
CCC

I know how to edit those three lines using CTRL_V and then SHIFT_I, but my question is how do i edit the first and the last line and leave out the second line that is:

test_AAA
BBB
test_CCC

thanks in advance.

2 Answers

Vim doesn't have a built-in "multiple cursors" feature. If you want to do it the vanilla way, you can use :help .:

" cursor is on first A
> AAA
  BBB
  CCC

" do itest_<Esc>
> test_AAA
  BBB
  CCC

" move the cursor to CCC
  test_AAA
  BBB
> CCC

" press .
  test_AAA
  BBB
> test_CCC

or the almighty :help :global, which is the de-facto way to operate on non-contiguous line:

:g/^AA|^CC/s/^/test_

And there are many other ways.

Or you can ask your favourite engine to help you find the couple of plugins that replicate that feature: "vim multiple cursors".

You could use a reverse global command:

:v/^BBB/norm Itest_

Explanation:

The normal global command uses "g" instead of "v" which inverts the search, so, for each line that does not start with BBB use the normal command "I" (insert)

Related