Let's say I had a block of text...
Lorem ipsum
Lorem ipsum dolor
Lorem ipsum dolor sit
Lorem ipsum dolor sit amet
...and I wanted to insert a .
at the end of each line. What would the best approach be to achieve this?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI do the following to append text to multiple lines:
<c-V>
- Enter Visual Block mode.j
/k
to select the lines.$
- Move cursor to last character.A
- Enter insert mode after last character.<Esc>
- Exit insert mode and finish block append.When compared to writing :norm
after selection there are even less key presses, but the real reason why I use this is because it's more instinctive for me to work on Visual Block mode for this type of changes.
On first line just type:
4:norm A.
4
and :
create a range for you and then norm A.
adds the dot to each line
Another solution for longer paragraphs could be:
Vip<C-v>$A.<Esc>
The first step is to select the paragraph with Vip
then you change to visual block mode and move the cursor to the end of each line with $
then you add the .
to each line with A.
and got to normal mode with <Esc>
to see the change be applied to each line.
First you can visually select them using V
and then use the movement keys to select the entire text (or if the text were strictly a paragraph you could do [starting at the first line] V}
). And then type :
. This should bring up something like :'<,'>
in the command-line.
:'<'>normal A.
This executes normal A.
on the selected lines. normal A.
executes A.
as normal mode keystrokes. This A
ppends a .
at the end of each of the selected lines.
If you want to add dot at the end of all lines:
:%norm A.
or you can use search and replace:
:%s/$/./
% is whole file, $ is end of line
:1,4s/$/./
which substitutes last character with a dot on lines one through four inclusive.
To make it a little more general, you can record a macro on a single line, and then play it back on each line in your visually selected range.
Record a macro into a register (the q register in this example):
qqA.<Esc>q
You can then visually select the lines you want to operate on:
Vip
And then play back your macro on each line (the range will be auto-filled):
:'<,'>norm @q