11

Say I have a comment like this:

//This is a comment

And I want to make it like this:

//*****************
//This is a comment
//*****************

Is there an easy way to insert a character repeatedly until even with the next/previous line?

2
  • 1
    To generate patterns like this you might want to use a snippet engine. You can read this question for a comparison of the snippets plugins. For example with ultisnips you could do ibox<key>This is a comment (where <key> is a key you define to trigger snippets) and that will create a box of the right size with this is a comment inside.
    – statox
    Dec 30, 2016 at 10:33
  • (17a*<Esc> would be something you could use In a similar situation where you know the number of characters you need, or the exact number doesn't matter because it doesn't have to be evenly aligned with another line.) Jan 14, 2017 at 16:36

2 Answers 2

12

Short answer

v $ r *

:help v_r


Longer answer

y y p / P     Duplicate line to below/above

2 l                    Move to position from where to overwrite (in this case 3rd column ← 2 to the right)

v $                  Select till end of line

r                          Replace all selected characters by char…

*

2
  • Addition to top voted answer Don't forget to use . to repeat the pattern on the other line! If you copy the line above and below, use v$r* on one of the lines, navigate to the other and press . (don't have enough rep for a comment)
    – Niels
    May 3, 2020 at 17:44
  • @Niels or duplicate the first overwritten line with yyjp Mar 8, 2022 at 1:28
3

At the occasion°:

Insert mode mappings to replicate the remainder of adjacent line

" Insert the rest of the line below the cursor.
" Mnemonic: Elevate characters from below line
:inoremap <A-e> 
    \<Esc>
    \jl
        \y$
    \hk
        \p
        \a
" Insert the rest of the line above the cursor.
" Mnemonic:  Y depicts a funnel, through which the above line's characters pour onto the current line.
:inoremap <A-y> 
    \<Esc>
    \kl
        \y$
    \hj
        \p
        \a

Copying single characters from neighboring lines in Insert mode

… is built-in: :help i_CTRL-E:

CTRL-E   Insert the character which is below the cursor.
CTRL-Y   Insert the character which is above the cursor.

° Not what the question text was about, but what other users might be looking for here, going from the current question title "insert until even with next line".

2
  • Are there similar keys for copying character from neighboring cells left/right?
    – Moberg
    Oct 27, 2021 at 6:20
  • @Moberg Not equally short ones built-in. In Normal mode, you can do ylP to duplicate the char the cursor is on; and yhp to duplicate the one to the left. You can add a count before the p/P. You could put for example the following mappings for Insert mode in your vimrc: :inoremap <A-l> <Esc>lylPi|:inoremap <A-h> <Esc>ylpa (By the way, in Normal mode, you can flip chars with xp) Mar 8, 2022 at 2:06

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.