0

I would like to yank one character and paste it in the first position of the succession of lines. For example, I need to add a "#" in the front of 7 lines of a file.

Someone can help me?

2 Answers 2

1

It depends largely on what you're calling vi. If it is really original-vi, then you could do this by

  • typing yl to "yank" a character starting at the cursor position, going one cell to the right.
  • moving the cursor to the first line where you want to put text
  • repetitively typing 0PEnter, going through the seven lines.

In original-vi, you could only affect a range of lines using ex mode. The ex mode of vi does accept a range, but the analogous command

:1,7P

would not put a character, but attempt to do something with lines. Rather, using ex mode, you would do a substitute, e.g.,

:1,7s/^/X/

but there is no way for a register value (the character(s) which you yanked) to be used in the substitution.

Further reading:

1
  • Thank you for your answer!! I will use the first method of your answer...
    – bmz
    Commented Feb 3, 2016 at 9:49
1

Note: this answer works for VIM only, not for VI.

You can yank it in a column by:

  1. Starting in normal mode (not insert).
  2. Go to the first row and column (where you want to insert the text).
  3. Press Ctrl+V to enter in vertical select mode.
  4. When having selected all lines in front of which you want to paste your text, press Shift+I.
  5. Now you can either
    1. paste your yanked text with Ctrl+R, 0 (or another register).
    2. or write any text.
  6. When finished, press Esc, and the same text will appear in all the selected lines.

Side note: you can also ask your Vi(m) questions on: vi.stackexchange.com

2
  • That's for vim, which is not vi. Commented Jan 29, 2016 at 9:37
  • Thank you for your answer!! I think that it is a god's level of vim!! WOW!!
    – bmz
    Commented Feb 3, 2016 at 9:48

You must log in to answer this question.

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