9

I have some lines of text like this:

firstName
lastName
email

Now I add private readonly string to the start of the first line. How do I repeat this edit for the other lines? (not manually visiting each line of course) (BTW I do realize this could be done with a regex, but I'm looking for a "vi way" if possible)

Just in case, I'm using VsVim

6 Answers 6

9

Navigate up to firstName in Normal mode and type

qaIprivate readonly string <ESC>jq

This will record the macro (in register a) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a on the lastName line, which will execute the macro twice more.


This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.

If it's the typing of private readonly string that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use . in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.

3
  • cool, I was actually looking for a way to repeat . but I guess it's not possible in this case... Mar 11, 2011 at 21:38
  • 1
    @muasch: Well, you can, you'd just use qa.jq as your macro (repeat command, then go one line down), and use 2@a to replay it. It's a little messy though.
    – Zeke
    Mar 11, 2011 at 21:47
  • +1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
    – Johan
    Mar 17, 2011 at 7:48
8

Another way to do this is to using Visual Block mode:

  1. Start with your cursor at the beginning of the first line.
  2. Press Ctrl+v to enter Visual Block mode.
  3. Press j to move down, extending the selection to include the lines you want.
  4. Press I to go into (prepending) Insert mode.
  5. Type private readonly string.
  6. Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
1
  • To extend the selection to every line in the file, step 1 could be gg and step 3 could be G0. Mar 12, 2011 at 1:24
8

:normal is great (and shorter) too:


:.,+2norm Iprivate readonly string 

  • .,+2 - from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
  • norm - type these commands as if I typed them in normal (command) mode
  • I(...) - insert the following string in the beginning of the line
1
  • +1 did not know about :norm, it's quite nice
    – Johan
    Mar 17, 2011 at 7:58
3

Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.

2

For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):

  1. Insert the text as you would normally do using I in the first line.

  2. Turn on the visual mode by pressing v or V or <c-v>.

  3. Grab all the lines you want to apply the change. In this case, jj will do.

  4. Hit :norm . then enter.

1

Give this a try:

:%s/^/private readonly string /

I suppose you could write a script, but why?

1
  • sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim. Mar 11, 2011 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .