Find and replace strings in vim on multiple lines

前端 未结 11 1782
耶瑟儿~
耶瑟儿~ 2020-12-02 03:29

I can do :%s///g for replacing a string across a file, or :s///

相关标签:
11条回答
  • 2020-12-02 04:00
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    /sys/sim/source/gm/kg/jl/ls/owow/lsal
    

    Suppose if you want to replace the above with some other info.

    COMMAND(:%s/\/sys\/sim\/source\/gm\/kg\/jl\/ls\/owow\/lsal/sys.pkg.mpu.umc.kdk./g)

    In this the above will be get replaced with (sys.pkg.mpu.umc.kdk.) .

    0 讨论(0)
  • 2020-12-02 04:05

    As a side note, instead of having to type in the line numbers, just highlight the lines where you want to find/replace in one of the visual modes:

    • VISUAL mode (V)
    • VISUAL BLOCK mode (Ctrl+V)
    • VISUAL LINE mode (Shift+V, works best in your case)

    Once you selected the lines to replace, type your command:

    :s/<search_string>/<replace_string>/g
    

    You'll note that the range '<,'> will be inserted automatically for you:

    :'<,'>s/<search_string>/<replace_string>/g
    

    Here '< simply means first highlighted line, and '> means last highlighted line.

    Note that the behaviour might be unexpected when in NORMAL mode: '< and '> point to the start and end of the last highlight done in one of the VISUAL modes. Instead, in NORMAL mode, the special line number . can be used, which simply means current line. Hence, you can find/replace only on the current line like this:

    :.s/<search_string>/<replace_string>/g
    

    Another thing to note is that inserting a second : between the range and the find/replace command does no harm, in other words, these commands will still work:

    :'<,'>:s/<search_string>/<replace_string>/g
    :.:s/<search_string>/<replace_string>/g
    
    0 讨论(0)
  • 2020-12-02 04:07

    The :&& command repeats the last substitution with the same flags. You can supply the additional range(s) to it (and concatenate as many as you like):

    :6,10s/<search_string>/<replace_string>/g | 14,18&&
    

    If you have many ranges though, I'd rather use a loop:

    :for range in split('6,10 14,18')| exe range 's/<search_string>/<replace_string>/g' | endfor
    
    0 讨论(0)
  • 2020-12-02 04:10

    Specifying the range through visual selection is ok but when there are very simple operations over just a couple of lines that can be selected by an operator the best would be to apply these commands as operators.

    This sadly can't be done through standards vim commands. You could do a sort of workaround using the ! (filter) operator and any text object. For example, to apply the operation to a paragraph, you can do:

    !ip
    

    This has to be read as "Apply the operator ! inside a paragraph". The filter operator starts command mode and automatically insert the range of lines followed by a literal "!" that you can delete just after. If you apply this, to the following paragraph:

    1
    2  Repellendus qui velit vel ullam!
    3  ipsam sint modi! velit ipsam sint
    4  modi! Debitis dolorum distinctio
    5  mollitia vel ullam! Repellendus qui
    6  Debitis dolorum distinctio mollitia
    7  vel ullam! ipsam
    8
    9  More text around here
    

    The result after pressing "!ap" would be like:

    :.,.+5
    

    As the '.' (point) means the current line, the range between the current line and the 5 lines after will be used for the operation. Now you can add the substitute command the same way as previously.

    The bad part is that this is not easier that selecting the text for latter applying the operator. The good part is that this can repeat the insertion of the range for other similar text ranges (in this case, paragraphs) with sightly different size. I.e., if you later want to select the range bigger paragraph the '.' will to it right.

    Also, if you like the idea of using semantic text objects to select the range of operation, you can check my plugin EXtend.vim that can do the same but in an easier manner.

    0 讨论(0)
  • 2020-12-02 04:14

    VI search and replace command examples

    Let us say you would like to find a word called “foo” and replace with “bar”.

    First hit [Esc] key

    Type : (colon) followed by %s/foo/bar/ and hit [Enter] key

    :%s/foo/bar/

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