56

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?

5 Answers 5

60

I do the following to append text to multiple lines:

  • <c-V> - Enter Visual Block mode.
  • Use j/k to select the lines.
  • $ - Move cursor to last character.
  • A - Enter insert mode after last character.
  • Insert desired text.
  • <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.

2
18

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.

3
  • What is this :A command? I did not find it via :h :A Mar 21, 2017 at 22:19
  • my bad. In fact didn't test the answer. I'm updating it. It should use the normal command just like @Kritixi Lhitos answer
    – rbernabe
    Mar 22, 2017 at 0:38
  • Two characters less than 4:norm A. would be 4:s/$/.
    – Philippos
    Mar 22, 2017 at 7:45
7

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 Appends a . at the end of each of the selected lines.

6

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
  • Since @vim.ryan asked for a block of text instead of the whole file, :1,4s/$/./ which substitutes last character with a dot on lines one through four inclusive.
    – rjt
    Oct 14, 2018 at 13:42
4

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

Your Answer

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

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