sponsor Vim development Vim logo Vim Book Ad

basic Tip #194: Inserting text in multiple lines

 tip karma   Rating 327/105, Viewed by 4698 

created:   January 8, 2002 7:52      complexity:   basic
author:   Gergely Kontra      as of Vim:   5.7

Do you know the I key in visual-block mode?

Suppose you have
let a=2
let b=3
let c=4

You want to make these variables script-wise. Then you move to over a, hit <C-v>, press jj and now press I.
You will be in insert mode before a
Now enter s:<Esc>, and when you press <Esc>, b and c will have the s: prefix too.
See |v_b_I|

Happy vimming!
Gergely Kontra

 rate this tip  Life Changing Helpful Unfulfilling 

<<Insert the current filename at cursor postion. | Switching between files >>

Additional Notes

[email protected], January 8, 2002 15:17
The above tip represents a concept that has saved me TONS of time over the past year or so. I use it daily.

There are lots of other neat things you can do with block selects. One of my favorites is adding a string to the end of each line... like so:

1. Press Ctrl+V and then j to extend the selection to all the lines you want

2. Press $. This will create a 'ragged edge' selection that extends to the right end of each line in your selection.

3. Press A. I inserts text at the beginning of a block selection, A appends text at the end of a block selection.

4. Type your text and press Esc.

Presto! You could also do something like this with :.,+10s/$/newtext ... but I've become a big fan of visual selections because they avoid line counting.

Also, the usual "c," "d," "y," and "r" commands work as you'd expect to change/delete/yank/replace stuff in blocks. So cool.
[email protected], August 2, 2002 8:18
Thanx

Remember Win32 users you probably need to use <C-Q> as you've  probably mapped <C-V> to Paste
Anonymous, April 23, 2003 12:16
Holy @#$# !! I've been using vim for years, and there's infinite stuff to learn. This tip is phenomenal !! Both the smart way to insert comments at the beginning, and the one to append something to the end of every line.  When you edit a config file that has 2 or more columns, and you want to append a new column, the second tip comes in very handy. Thanks!
Anonymous, May 6, 2003 13:49
Hoooo Raah! Very nice indeed!
[email protected], March 29, 2004 7:56
The more I use vim the more I love it.
Thank you.

*shout* wohoooo :)
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo