Search code examples
vimformatting

VIM: Copying Formatting to Visual Selection?


I'm not sure if this is possible, but Vim constantly surprises me. What I'd like to be able to do is take the formatting of one block of text and apply it to a selection. Assuming several lines like this:

<li><a href="#"><span>Something Here</span><i class="icon"></i></a></li>
<li><a href="#"><span>Something Here</span><i class="icon"></i></a></li>
<li><a href="#"><span>Something Here</span><i class="icon"></i></a></li>

I'd like to format one of the lines:

<li>
    <a href="#">
        <span>Something Here</span>
        <i class="icon"></i>
    </a>
</li>

And then apply that formatting to the remaining lines. Again, no clue if this is doable, but it would be very neat if it could - I often have to implement HTML templates where there are long lines that may have 5 or 6 nested tags within, often starting with an indentation that is quite out there.


Solution

  • Do you know the recording feature of vim? Try to do following:

    1. press q + some character to enter the recording mode
    2. do formatting of a line
    3. press q to exit the recording mode
    4. go to the next line
    5. press @ + some character to reproduce recorded operations