How to edit multiple locations simultaneously in Vim

后端 未结 6 807
悲&欢浪女
悲&欢浪女 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: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.

提交回复
热议问题