How to edit multiple locations simultaneously in Vim

后端 未结 6 799
悲&欢浪女
悲&欢浪女 2021-02-04 14:06

In certain text editors, like E, I can select multiple locations and, as I type, all the selected locations get replaced with the characters I am typing.

For example i

相关标签:
6条回答
  • 2021-02-04 14:25

    For your example I would use a substitution:

    :%s/class=""/class="something"/g
    
    0 讨论(0)
  • 2021-02-04 14:30

    You may be looking for visual mode blockwise, which will allow insertion, deletion etc on several lines at once.

    Blockwise mode will allow square selections with the column and line of the initial point in one corner, and the current cursor position defining the column and line of the other corner. This, as opposed to the line based selection that is the default.

    CTRL-v will place you in blockwise visual mode.

    If you have several lines like so:

    INSERT INTO Users VALUES(1, 'Jim');
    INSERT INTO Users VALUES(2, 'Jack');
    INSERT INTO Users VALUES(3, 'Joseph');
    

    And wanted to insert "0," after the id for each line, then place the cursor after the comma in the first line:

    INSERT INTO Users VALUES(1,* 'Jim');
    

    With the asterisk representing the cursor the command sequence would be:

    CTRL-v  # Put into blockwise visual mode
    j       # Down a line
    j       # Down a line
    CTRL-I  # Captial I for insert
    0,      # the text to insert
    Esc     # escape
    

    The text should now look like:

    INSERT INTO Users VALUES(1, 0, 'Jim');
    INSERT INTO Users VALUES(2, 0, 'Jack');
    INSERT INTO Users VALUES(3, 0, 'Joseph');
    

    Also blockwise visual mode, x will delete a selection, y will yank it.

    :help CTRL-V will give further documentation.

    0 讨论(0)
  • 2021-02-04 14:32

    Have a look at SnippetsEmu. It should be doing something very similar to what you need.

    It emulates TextMates snippets. You should be able to have one snippet with the same tag repeated, and editing will do the right thing, updating the same tag in all locations, as you type.

    0 讨论(0)
  • 2021-02-04 14:38

    I'm also looking for something like that, more specifically a very useful functionality from ST2, where you press CTRL+D to select next occurrence and then replace both occurrence by just typing it.

    0 讨论(0)
  • 2021-02-04 14:47

    Try vim-multiple-cursors.

    Press Ctrl+N as many times as necessary to select the multiple occurences.

    0 讨论(0)
  • 2021-02-04 14:48

    Here's how I would probably edit those particular lines (there are many ways):

    /""<enter>
    aText to replace...<esc>
    n
    .
    

    First, search for the empty quotes to put the cursor on the first one. Using the "a" (append) command, type the new text to put inside the quotes. When you're done, use "n" (next) to go to the next instance, and "." (repeat last command) to insert the same text again. Repeat the "n ." as many times as necessary.

    This method takes less up-front preparation and lets you get started right away without identifying ahead of time all the locations where you might want to add the text.

    0 讨论(0)
提交回复
热议问题