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
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:
- ex (POSIX)
- vi (POSIX)
- 4. EX COMMAND MODE (Elvis editor)
- 5. Introducing the ex Editor (from Learning the vi Editor)
Note: this answer works for VIM only, not for VI.
You can yank it in a column by:
- Starting in normal mode (not insert).
- Go to the first row and column (where you want to insert the text).
- Press Ctrl+V to enter in vertical select mode.
- When having selected all lines in front of which you want to paste your text, press Shift+I.
- Now you can either
- paste your yanked text with Ctrl+R, 0 (or another register).
- or write any text.
- 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