Question

I want to select multiple lines in vim and whatever I type should be written on those lines.

It is easy to do in Notepad++ using shift-alt to select the cursor points and start typing.

For eg.

  17  #define       M_SPI_BASE              48098000
  18  #define       SPI2_BASE               4809A000
  19  #define       SPI3_BASE               0x480B8000
  20  #define       SPI4_BASE               0x480BA000

As in line numbers 19 and 20, I want to add 0x in front of the addresses 48098000 & 4809A000 in line 17 & 18 to get 0x48098000 & 0x4809A000 respectively.

I want to select my cursor at two different points i.e., before 48098000 & 4809A000 and type 0x.

How do I do it in vim?

Was it helpful?

Solution

Look into Visual Blockmode

With your cursor before 48098000 you can use following sequence

ctrl+vjI0xEsc

What might trip you up is that the changes to other rows are only made after leaving visual blockmode, not while typing them.

Breakdown:

  • ctrl+v Start Visual mode Blockwise
  • j Select downwards
  • I Start insert
  • 0x Add text
  • Esc Leave Visual mode. Vim will add the text you've typed to the selection.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top