NSK
NSK

Reputation: 186

How to edit multiple lines when new line is also involved in VIM? (or) <VIM Multiple Line edit Not working>

I want to edit multiple lines in VIM.I know there exists Visual block editing(i.e CTRL+V ----> I -----> <Make required change> -----> ESC). But Surprisingly it is not working for the case of entering a new line.

For example I want to make the following code

if(i==1):
if(i==2):

to something like

if(i==1):
  print("say Hello")
if(i==2):
  print("say Hello")

Hoping for a way to do it without too many commands. Thanks in advance. :)

Upvotes: 1

Views: 480

Answers (1)

romainl
romainl

Reputation: 196476

The simplest approach is to use :help .

  1. Open a new line below the first line and type what you need:

    o  print("say Hello")
    
  2. Leave insert mode:

    <Esc>
    
  3. Move the cursor down by one line:

    j
    
  4. And repeat the last edit:

    .
    

In short:

o  print("say Hello")<Esc>j.

If you absolutely want to use visual mode, here is another way:

  1. Select the lines:

    vj
    
  2. Press : to enter command-line mode, with the range corresponding to the visual selection automatically inserted for you:

    :'<,'>
    
  3. Use :help :s to substitute the EOL with a newline followed by the desired text on each line in the range:

    :'<,'>s/$/\r  print("say Hello")<CR>
    

Upvotes: 1

Related Questions