TransWikia.com

Append single-line register to many lines

Vi and Vim Asked by Mat M on December 26, 2021

I have a file like

...
one;line;with;many;data
starter
another_starter
yet_to_end
complete;line;providing;information
...
    

and I would like to have:

...
one;line;with;many;data
starter;default;selected;data;for;init
another_starter;default;selected;data;for;init
yet_to_end;default;selected;data;for;init
complete;line;providing;information
...
    

My best try was to

# get "b register filled by a previously edited file
# b reg is: ;default;selected;data;for;init
/starter  # go to the block to modify
Shift-V  # Select-Line mode
2j # Select other lines
$ # move to end of line
A # expected to enter append mode, but goes to begin of block
A # actually going append mode
Ctrl-V Ctrl-R b # append b register

I expanded it from https://vim.fandom.com/wiki/Inserting_text_in_multiple_lines#Append

Last operation actually paste only on the first line of the selection, so I have:

...
one;line;with;many;data
starter;default;selected;data;for;init
another_starter
yet_to_end
complete;line;providing;information
...
    

It looks like a standard pattern in my view, but I failed to find a working solution, either by myself and on internet.
I am on a production system and using vim-minimal-7.2.411-1.8.el6.x86_64, if it helps for diagnostics and solution.

One Answer

Given "b contains what you wish to append, I would

  1. select V + movement

  2. use :s on the end of line with :s-= to insert the content of @b

     " '<,'> will be automatically added when hitting `:`  from the visual mode
     :'<,'>s/$/=@b/
    

As @D. Ben Knoble pointed out, if the lines that need to be completed are exactly all the lines without a semicolon, range selection and expression could be replaced with the inVerted-:global command. IOW

:v/;/s/$/=@b/

Answered by Luc Hermitte on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP