13

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?

8

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.

7

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.

  • What is this :A command? I did not find it via :h :A – Peter Rincker Mar 21 '17 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 '17 at 0:38
  • Two characters less than 4:norm A. would be 4:s/$/. – Philippos Mar 22 '17 at 7:45
3

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.

3

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

  • 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 '18 at 13:42
1

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, privacy policy and cookie policy

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