Vim: insert the same characters across multiple lines

后端 未结 12 1880
慢半拍i
慢半拍i 2020-11-29 14:23

Sometimes I want to edit a certain visual block of text across multiple lines.

For example, I would take a text that looks like this:

name
comment
ph         


        
相关标签:
12条回答
  • 2020-11-29 14:50

    Another approach is to use the . (dot) command in combination with I.

    1. Move the cursor where you want to start
    2. Press I
    3. Type in the prefix you want (e.g. vendor_)
    4. Press esc.
    5. Press j to go down a line
    6. Type . to repeat the last edit, automatically inserting the prefix again
    7. Alternate quickly between j and .

    I find this technique is often faster than the visual block mode for small numbers of additions and has the added benefit that if you don't need to insert the text on every single line in a range you can easily skip them by pressing extra j's.

    Note that for large number of contiguous additions, the block approach or macro will likely be superior.

    0 讨论(0)
  • 2020-11-29 14:51
    1. Move the cursor to the n in name.
    2. Enter visual block mode (Ctrlv).
    3. Press j three times (or 3j).
    4. Press I (capital i).
    5. Type in vendor_. Note: It will only update the screen in the first line - until Esc is pressed (6.), at which point all lines will be updated.
    6. Press Esc.

    mini-screencast demonstrating the method

    An uppercase I must be used rather than a lowercase i, because the lowercase i is interpreted as the start of a text object, which is rather useful on its own, e.g. for selecting inside a tag block (it):

    mini-screencast showing the usefulness of the 'it' text object

    0 讨论(0)
  • 2020-11-29 14:51
    1. Select the lines you want to modify using CtrlV.
    2. Press:

      • I: Insert before what's selected.
      • A: Append after what's selected.
      • c: Replace what's selected.
    3. Type the new text.

    4. Press Esc to apply the changes to all selected lines.
    0 讨论(0)
  • 2020-11-29 14:56

    I wanted to comment out a lot of lines in some config file on a server that only had vi (no nano), so visual method was cumbersome as well Here's how i did that.

    1. Open file vi file
    2. Display line numbers :set number! or :set number
    3. Then use the line numbers to replace start-of-line with "#", how?

    :35,77s/^/#/

    Note: the numbers are inclusive, lines from 35 to 77, both included will be modified.

    To uncomment/undo that, simply use :35,77s/^#//

    If you want to add a text word as a comment after every line of code, you can also use:

    :35,77s/$/#test/ (for languages like Python)

    :35,77s/;$/;\/\/test/ (for languages like Java)

    credits/references:

    1. https://unix.stackexchange.com/questions/84929/uncommenting-multiple-lines-of-code-specified-by-line-numbers-using-vi-or-vim

    2. https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once

    0 讨论(0)
  • 2020-11-29 14:58

    An alternative that can be more flexible:

    Example: To enter the text XYZ at the beginning of the line

    :%norm IXYZ
    

    What's happening here?

    • % == Execute on every line
    • norm == Execute the following keys in normal mode
    • I == Insert at beginning of line
    • XYZ == The text you want to enter

    Then you hit Enter, and it executes.

    Specific to your request:

    :%norm Ivendor_
    

    You can also choose a particular range:

    :2,4norm Ivendor_
    

    Or execute over a selected visual range:

    :'<,'>norm Ivendor_
    
    0 讨论(0)
  • 2020-11-29 15:00

    I would use a macro to record my actions and would then repeat it.

    1. Put your cursor on the first letter in name.
    2. Hit qq to start recording into the q buffer.
    3. Hit i to go into insert mode, type vector_, and then hit Esc to leave insert mode.
    4. Now hit 0 to go back to the beginning of the line.
    5. Now hit j to go down.
    6. Now hit q again to stop recording.

    You now have a nice macro.

    Type 3@q to execute your macro three times to do the rest of the lines.

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