456

Sometimes I want to edit a certain visual block of text across multiple lines.

For example, I would take a text that looks like this:

name
comment
phone
email

And make it look like this

vendor_name
vendor_comment
vendor_phone
vendor_email

Currently the way I would do it now is...

  1. Select all 4 row lines of a block by pressing V and then j four times.
  2. Indent with >.
  3. Go back one letter with h.
  4. Go to block visual mode with Ctrlv.
  5. Select down four rows by pressing j four times. At this point you have selected a 4x1 visual blocks of whitespace (four rows and one column).
  6. Press C. Notice this pretty much indented to the left by one column.
  7. Type out a " vendor_" without the quote. Notice the extra space we had to put back.
  8. Press Esc. This is one of the very few times I use Esc to get out of insert mode. Ctrlc would only edit the first line.
  9. Repeat step 1.
  10. Indent the other way with <.

I don't need to indent if there is at least one column of whitespace before the words. I wouldn't need the whitespace if I didn't have to clear the visual block with c.

But if I have to clear, then is there a way to do what I performed above without creating the needed whitespace with indentation?

Also why does editing multiple lines at once only work by exiting out of insert mode with Esc over Ctrlc?


Here is a more complicated example:

name    = models.CharField( max_length = 135 )
comment = models.TextField( blank = True )
phone   = models.CharField( max_length = 135, blank = True )
email   = models.EmailField( blank = True )

to

name    = models.whatever.CharField( max_length = 135 )
comment = models.whatever.TextField( blank = True )
phone   = models.whatever.CharField( max_length = 135, blank = True )
email   = models.whatever.EmailField( blank = True )

In this example I would perform the vertical visual block over the ., and then reinsert it back during insert mode, i.e., type .whatever.. Hopefully now you can see the drawback to this method. I am limited to only selecting a column of text that are all the same in a vertical position.

5
  • 13
    Better way: ":%s/^/vendor_/" Mar 3, 2012 at 20:47
  • 2
    Thanks for the response. That works if I only want to append a word to every line. But the method I use can be done at any position. Let me add another my example with a more complicated example.
    – hobbes3
    Mar 3, 2012 at 20:49
  • 4
    Then highlight the lines you want to change with shift-V, and type an appropriate search and replace command - in the second example, :s/models\./\0whatever./ Mar 3, 2012 at 21:06
  • 1
    Many ways to achieve the above. To fully answer all parts of question then part 1) either regex or visual mode I edit esc works nicely as offered in answers (I prefer visual selection in GVIM). For part 2), macro is perhaps easiest as offered by Brian Neal. The best resource I know of for all these kinds of "how tos" is Practical Vim by Drew Neil. Excellent quick read and light weight reference whenever you forget a formula. Hope this recommendation helps someone, it will be well worth the effort for many vim users.
    – arcseldon
    Nov 12, 2013 at 10:22
  • 1
    @Paul: And if you’re counting keystrokes, you could replace \0 with &.
    – icktoofay
    Aug 8, 2014 at 3:28

15 Answers 15

1235
  1. Move the cursor to the n in name.
  2. Enter visual block mode (Ctrlv).
  3. Press j three times (or 3j) to jump down by 3 lines; G (capital g) to jump to the last line
  4. Press I (capital i).
  5. Type in vendor_. Note: It will only update the screen in the first line - until Esc is pressed (6.), at which point all lines will be updated.
  6. Press Esc.

mini-screencast demonstrating the method

An uppercase I must be used rather than a lowercase i, because the lowercase i is interpreted as the start of a text object, which is rather useful on its own, e.g. for selecting inside a tag block (it):

mini-screencast showing the usefulness of the 'it' text object

22
  • 22
    I couldn't get this to work in Windows (since CTRL+V is mapped for clipboard paste). But Vim has also mapped this to Ctrl+Q for the Windows version.
    – Arnestig
    Jul 3, 2014 at 11:11
  • 4
    @Ven: Vim’s not actually busy during that time—it’s just waiting for you to press another key, and eventually times out. Two experiments for you: first, try pressing escape and then immediately pressing another key, like j or ^L. You should see that Vim wasn’t busy after all; it was just waiting. Second, try changing 'timeout' to a lower value, like 80 (rather than the default 1000) and observe that it times out more quickly.
    – icktoofay
    Aug 8, 2014 at 3:24
  • 2
    @icktoofay It looks like the variable you mean to set is timeoutlen not timeout.
    – jez
    Aug 29, 2014 at 17:39
  • 6
    @ANjaNA: If where you want to insert is all in the same column (e.g. if the numbers are all the same length), you can use the same technique except pressing A rather than I, which will insert after the selected block rather than before. If your lines are different lengths, though, that won’t work; I like to use a substitution matching the end of each line for that, e.g. selecting some lines and using :s/$/inserted/.
    – icktoofay
    Sep 21, 2015 at 23:03
  • 5
    I had to press down arrow after pressing esc to get this to work
    – GeoSword
    Dec 5, 2017 at 12:24
113

Another approach is to use the . (dot) command in combination with i.

  1. Move the cursor where you want to start
  2. Press i
  3. Type in the prefix you want (e.g. vendor_)
  4. Press esc.
  5. Press j to go down a line
  6. Type . to repeat the last edit, automatically inserting the prefix again
  7. Alternate quickly between j and .

I find this technique is often faster than the visual block mode for small numbers of additions and has the added benefit that if you don't need to insert the text on every single line in a range you can easily skip them by pressing extra j's.

Note that for large number of contiguous additions, the block approach or macro will likely be superior.

4
  • 1
    I prefer this for only a few lines. Much easier to execute IMO. Mar 21, 2014 at 18:19
  • 1
    After you type in the prefix you want, don't you then need to press escape key?
    – iamnotsam
    Aug 4, 2014 at 14:00
  • 1
    @iamnotsam, if you are in insert mode, then yes you need to press Esc before moving to the next line. Otherwise pressing "." will simply insert a period. Dec 23, 2021 at 13:49
  • this is amazing. also works with deleting things.
    – gMale
    Mar 9 at 2:38
77
  1. Select the lines you want to modify using CtrlV.
  2. Press:

    • I: Insert before what's selected.
    • A: Append after what's selected.
    • c: Replace what's selected.
  3. Type the new text.

  4. Press Esc to apply the changes to all selected lines.
4
  • 6
    Excellent! I'd just highlight the final step Esc for VIM noobs like myself - it took me a while to figure out that the changes get propagated "later"!
    – Bugs Bunny
    May 10, 2016 at 12:46
  • 2
    Nice, didn't know about "A" and "C" options too.
    – hobbes3
    May 24, 2018 at 1:11
  • 2
    Why doesn't this work for v? I can do it with Ctrl+V --> A, but doing v to select the text doesn't work -- A only appends to that line. Aren't they both selection? Also, very strange -- A only appends to the end of the line IF I extend the visual block to the end of the line; otherwise it appends wherever the visual block ends for each line. It's like it doesn't recognize a difference between a and A.
    – Alex G
    Nov 18, 2018 at 22:42
  • 2
    I needed to use c instead of C, but it worked perfectly fine. Thank you
    – Tarol
    May 7, 2020 at 11:40
42

I would use a macro to record my actions and would then repeat it.

  1. Put your cursor on the first letter in name.
  2. Hit qq to start recording into the q buffer.
  3. Hit i to go into insert mode, type vector_, and then hit Esc to leave insert mode.
  4. Now hit 0 to go back to the beginning of the line.
  5. Now hit j to go down.
  6. Now hit q again to stop recording.

You now have a nice macro.

Type 3@q to execute your macro three times to do the rest of the lines.

4
  • 1
    Is "q" special (for "quick macro"?)? Jan 1, 2020 at 18:37
  • 1
    How can the macro be made persistent (working across several sessions)? Jan 1, 2020 at 18:40
  • 1
    @PeterMortensen Vim has a feature called sessions that will persist buffers (and a lot of other things) that you can look into.
    – Brian Neal
    Jan 2, 2020 at 17:41
  • 1
    From one Peter to another, great answer! Jan 6, 2022 at 22:35
34
:%s/^/vendor_/

or am I missing something?

5
  • 1
    You're missing the fact that it's likely a subtext in a file with many other lines on which the vender_ prefix is unwanted. Mar 21, 2014 at 18:21
  • 14
    @dan If only a subtext is needed, you can limit the search to a set of line numbers, eg 1,4s/^/vendor_/
    – Brad Koch
    May 20, 2015 at 19:34
  • 2
    @BradKoch Interesting. I was not aware of the line number constraints, thanks for that. May 21, 2015 at 14:18
  • 11
    it is interesting, but what's more practical is to visually block select with V and type :. It will prefix the ed expression with :'<,'> which make the ed apply only to the selection with :'<,'>s/^/vendor_/
    – Dolanor
    Aug 9, 2016 at 17:28
  • 1
    if you highlight matches like I do you may need to run :noh after.
    – gabeio
    Mar 7, 2021 at 15:28
17

Updated January 2016

Whilst the accepted answer is a great solution, this is actually slightly fewer keystrokes, and scales better - based in principle on the accepted answer.

  1. Move the cursor to the n in name.
  2. Enter visual block mode (ctrlv).
  3. Press 3j
  4. Press I.
  5. Type in vendor_.
  6. Press esc.

visual illustration

Note, this has fewer keystrokes than the accepted answer provided (compare Step 3). We just count the number of j actions to perform.

If you have line numbers enabled (as illustrated above), and know the line number you wish to move to, then step 3 can be changed to #G where # is the wanted line number.

In our example above, this would be 4G. However when dealing with just a few line numbers an explicit count works well.

16

An alternative that can be more flexible:

Example: To enter the text XYZ at the beginning of the line

:%norm IXYZ

What's happening here?

  • % == Execute on every line
  • norm == Execute the following keys in normal mode (short for normal)
  • I == Insert at beginning of line
  • XYZ == The text you want to enter

Then you hit Enter, and it executes.

Specific to your request:

:%norm Ivendor_

You can also choose a particular range:

:2,4norm Ivendor_

Or execute over a selected visual range:

:'<,'>norm Ivendor_

Or execute for each line that matches a 'target' regex:

:%g/target/norm Ivendor_
3
  • 2
    is it possible to send key sequences like <Esc> while in insert mode (norm Iabc<ESC>) in order to do more interesting stuff?
    – batbrat
    Apr 30, 2019 at 15:56
  • 3
    @batbrat I just figured this out! You can hit ctrl-v ctrl-[ to insert an "escaped Esc", which pulls you back to "normal mode" for the purpose of your normal command. I just tested this with '<,'>norm Iaa^[ysiw", where ^[ is the escaped Esc. May 10, 2019 at 20:23
  • 1
    The :%mode approach is really valuable to know. I'm glad I stumbled upon this answer!
    – Adrian
    Sep 27, 2021 at 11:20
10

I wanted to comment out a lot of lines in some config file on a server that only had vi (no nano), so visual method was cumbersome as well Here's how i did that.

  1. Open file vi file
  2. Display line numbers :set number! or :set number
  3. Then use the line numbers to replace start-of-line with "#", how?

:35,77s/^/#/

Note: the numbers are inclusive, lines from 35 to 77, both included will be modified.

To uncomment/undo that, simply use :35,77s/^#//

If you want to add a text word as a comment after every line of code, you can also use:

:35,77s/$/#test/ (for languages like Python)

:35,77s/;$/;\/\/test/ (for languages like Java)

credits/references:

  1. https://unix.stackexchange.com/questions/84929/uncommenting-multiple-lines-of-code-specified-by-line-numbers-using-vi-or-vim

  2. https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once

1
  • 1
    This was the perfect solution for me as I'm often only using vim-tiny.
    – JSON C11
    Mar 12, 2019 at 21:37
10

You might also have a use case where you want to delete a block of text and replace it.

Like this

Hello World

Hello World

To

Hello Cool

Hello Cool

You can just visual block select "World" in both lines.

Type c for change - now you will be in insert mode.

Insert the stuff you want and hit escape.

Both get reflected vertically. It works just like 'I', except that it replaces the block with the new text instead of inserting it.

7

Suppose you have this file:

something

name
comment
phone
email

something else
and more ...

You want to add "vendor_" in front of "name", "comment", "phone", and "email", regardless of where they appear in the file.

:%s/\<\(name\|comment\|phone\|email\)\>/vendor_\1/gc

The c flag will prompt you for confirmation. You can drop that if you don't want the prompt.

5
Use Ctrl+V to enter visual block mode
Move Up/Down to select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line
3
  • 1
    does this method work for all versions of vim or require any vim plugins? the reason why i ask is b/c it sometimes works on some machines, but sometimes does not. i can't figure out the difference. vim that comes with git on windows - i can't get it working. on some ubuntu and centos linux machines, i can't get it working. i have had success in a few rare cases, but i could not reproduce the success. it's a mystery. nothing seems to happen after i hit ESC.
    – davidj411
    Nov 22, 2017 at 15:26
  • 2
    oddly, it DOES work for me now. nothing has changed, except maybe the stars :) while looking for solution to my issue, i did find something at this page though that mentions something to look for in vim vim.wikia.com/wiki/Inserting_text_in_multiple_lines In Vim, check that you have the blockwise-operators feature (I, A, and more) by entering the :version command. The output should include +visualextra.
    – davidj411
    Dec 7, 2017 at 15:35
  • 1
    I meant to add a comment in general, not to your answer. Most of these answers say the same thing but using different words. Mine had something different to offer and included a link.
    – davidj411
    Dec 10, 2017 at 0:50
4
  1. Ctrl + v to go to visual block mode
  2. Select the lines using the up and down arrow
  3. Enter lowercase 3i (press lowercase I three times)
  4. I (press capital I. That will take you into insert mode.)
  5. Write the text you want to add
  6. Esc
  7. Press the down arrow
1

I came here to paste in many lines an already copied string. When copy with y we can paste, in the INSERT MODE, pressing Ctrl+r and right after press ''. This will have the same result as being in NORMAL MODE and press p. This is called paste from registry.

Suppose the following text in the buffer:

vendor_something

text
to_receive
the_paste
pattern

Then we can put the cursor pointing to v in vendor_ and press v, move to right using l until select the underscore symbol we want to paste in the text bellow. After that, we can point the cursor at the beginning of "text" (two lines bellow vendor_something) and press Ctrl+v. Then I to go into INSERT MODE where we press 3j Ctrl+r '' Esc. The result of this sequence will be:

vendor_something

vendor_text
vendor_to_receive
vendor_the_paste
vendor_pattern
1

:.,+3s/^/vendor_/

Another example, I needed to just add two spaces to a block of 125 lines, so I used (with cursor positioned at the beginning of the first line of the block):

:.,+125s/^/ /

Worked great.

1

If the change is required in the entire file,

:1,$s/^/vendor_/

If the change is required for only a few lines,

Go to the first line where change is required, and either give the command

:.,ns/^/vendor_/

Substitute n with the line number of the last line in the block.

Or,

:.,+ns/^/vendor_/

Substitute n with number of lines minus 1 in which the change is required.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.