3

say for example, I have this text:

<div class="test1"></div>
<span class="highlight"></span>
<nav class="tester"></nav>
<aside class="wrapper"></aside>

I want use visual block mode to add a new class to each line - in one action - so the result is:

<div class="test1 new_class"></div>
<span class="highlight new_class"></span>
<nav class="tester new_class"></nav>
<aside class="wrapper new_class"></aside>

I have tried enter visual block mode Ctrl + v, select all 4 lines, but I cant move the cursor position to the 2nd " in each line, I have tried 2f" , which does find the 2nd " - for one particular line, but then the cursor position of that line dictates the position for all the other lines. I cant get the cursor position to find the 2nd " for each individual line.

I know another solution to this problem is to simply visually select the lines with regular visual and then use command

:'<,'>s/">/ new_class">/g 

this works, but is it possible to use -- VISUAL BLOCK -- to do this?

1
  • Although it's asking about selection lengths, I think the answers to this question will suit you. Oct 17, 2015 at 14:28

1 Answer 1

5

No, you can only do rectangular selections with visual-block mode.

Beside substitution, there are basically two ways to do what you want without third party plugins: direct repetition of normal mode commands and macros.

  1. Repeating normal mode commands

    Do your edit in one place and repeat it in the other places:

    /"<<CR>              search for ">
    s new_class"<Esc>    replace it with your text followed by a double quote
    n.                   jump to next occurrence and repeat the change
    n.                   and again
    n.                   and again
    

    See :help single-repeat.

  2. Replaying a recorded macro

    Record your macro and play it back as many times as necessary:

    qq                  start recording in register q
    02f"                go to first column then jump to second "
    i new_class<Esc>    insert your text
    j                   go down one line
    q                   stop recording
    @q                  replay your recording
    @q                  and again
    @q                  and again
    

    Notes:

    • you can use @@ to repeat the last played macro: @q@@@@,
    • you can use a count before @q: 3@q.

    See :help recording.

  3. Using a "manual" macro

    Use :{range}normal to execute a macro on lines covered by {range}:

    :'<,'>normal 2f"i new_class<CR>
    

    See :help :normal.

3
  • thanks, I thought this might have been the case, I just tried your first 2 options - and they worked nicely! option 1 technique using "> and then n to jump to next match is quite interesting, Ive noticed matches from searches tend to persist even after youve exited "search" mode. Ive just never been able to capitalise on it till now! Oct 17, 2015 at 12:56
  • 1
    It seems there is an exception to the "rectangular selections" rule, and that is when $ is used while the visual selection spans lines of different lengths. I assume this is the only exception with the built-in functionality. Oct 17, 2015 at 14:29
  • @joeytwiddle, yes you are right.
    – romainl
    Oct 17, 2015 at 14:31

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.