Find and replace strings in vim on multiple lines

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

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

相关标签:
11条回答
  • 2020-12-02 03:48

    Search and replace

    :%s/search\|search2\|search3/replace/gci
    

    g => global search

    c => Ask for confirmation first

    i => Case insensitive

    If you want direct replacement without confirmation, use below command

    :%s/search/replace/g
    

    If you want confirmation for each replace then run the below command

    :%s/search/replace/gc
    

    Ask for confirmation first, here search will be case insensitive.

    :%s/search/replace/gci
    
    0 讨论(0)
  • 2020-12-02 03:49

    We don't need to bother entering the current line number.

    If you would like to change each foo to bar for current line (.) and the two next lines (+2), simply do:

    :.,+2s/foo/bar/g
    

    If you want to confirm before changes are made, replace g with gc:

    :.,+2s/foo/bar/gc
    
    0 讨论(0)
  • 2020-12-02 03:54

    Replace All:

    :%s/foo/bar/g
    

    Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.

    For specific lines:

    :6,10s/foo/bar/g
    

    Change each 'foo' to 'bar' for all lines from line 6 to line 10 inclusive.

    0 讨论(0)
  • 2020-12-02 03:55

    To answer this question:

    :40,50s/foo/bar/g

    replace foo with bar in these lines between the 40th line and the 50th line(inclusive), when execute this command you can currently in any line.

    :50,40s/foo/bar/g

    also works, vim will ask you for comfirm and then do the replacement for you as if you have input the first command.

    :,50s/foo/bar/g

    replace foo with bar in these lines between the line you are currently in and the 50th line(inclusive). (if you are in a line AFTER the 50th line vim will ask for confirm again)

    To clearity the difference between vim and the vrapper plugin of Eclipse:

    Note that in varpper

    :,50s/foo/bar/g command will NOT work.

    :50,40s/foo/bar/g will work without asking for comfirmation.

    (For Vrapper Version 0.74.0 or older).

    0 讨论(0)
  • 2020-12-02 03:57

    In vim if you are confused which all lines will be affected, Use below

     :%s/foo/bar/gc  
    

    Change each 'foo' to 'bar', but ask for confirmation first. Press 'y' for yes and 'n' for no. Dont forget to save after that

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

    You can do it with two find/replace sequences

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

    The second time all you need to adjust is the range so instead of typing it all out, I would recall the last command and edit just the range

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