1

I often have code of the form:

    money = double(123)
    user_level = user["level"]
    timer = get_time()

and want to have:

    money = double(123)
    user_level = user["level"]
    timer = get_time()

    print(money)
    print(user_level)
    print(timer)

How can I go from the first to the second?

My first thought was to:

  1. move cursor to the "m" of "money"
  2. enter visual block mode with ^v
  3. select multiple lines with 3j
  4. move to first word with w
  5. yank with y
  6. paste with p
  7. use visual block mode to add print( prefix and ) suffix

However, "move to first word with w" is not per-line and results in a rectangular block (unwanted):

money
user_
timer
2
  • Your two recent questions indicate you want to use the :substitute command but you're not too familiar with it. Maybe time to read :help :substitute and :help pattern?
    – Friedrich
    Jan 28 at 20:26
  • Do you still have something open in your question? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons? It allow the question to rest :-) Jan 29 at 11:55

3 Answers 3

3

Indeed, with visual block selection one operates on rectangular block of text.

If it was aligned on = you would be able to do it, otherwise you can use suggested in other answers methods.

Similar one might be using :normal command without macro recording:

  1. Duplicate with yap, Go<ESC>p
  2. Select duplicated with vip
  3. Convert using :norm Iprint(^[f DA) where ^[ is generated by pressing CTRL-V Escape

asciicast

PS, I personally would just duplicated and manually edited the text if it is less than 5 lines.

1
  • 1
    Upvoted for the PS. For a few lines, typing it out is faster than to come up with a clever solution.
    – Friedrich
    Jan 29 at 9:15
1

You could:

  • Duplicate the line first

  • Apply the following macro 0Iprint(^[eld$a)^[

    • 0 go to the start of the line (when you run a macro on many lines it is a good practice not to assume anything about the initial position of the cursor)
    • I goes in edit mode to the first non white character
    • print( insert the initial print(
    • ^[ or Esc switch to Normal mode
    • eld$ delete the rest of the line
    • a) insert the final )
    • ^[ or Esc switch to Normal mode (when you run a macro on many line it is a good practice to comeback to Normal mode at the end of the macro)

If you record it with qa and stop recording with q you can repeat it in the other lines with @a.

You can also select the lines you want to apply the macro and run:

:'<,'>norm @a

Another technique to repeat the macro is to make the macro goes to the next line: 0Iprint(^[eld$a)^[j (notice the additional j at the end)

Then to run it on 2 lines you can do 2@a.

1
  • 1
    This is the way I imagine Drew Neill, who wrote "Practical Vim", would solve this problem.
    – reatter
    Feb 3 at 18:24
1

I would:

  • Duplicate the lines first

  • On the first line I would run the following substitution:

    :s/\v(\S+).*/print(\1)

  • I would repeat it using the & key on the other lines

Your Answer

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

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