vim search and replace using current line as reference point

后端 未结 2 1291
南旧
南旧 2021-01-30 10:50

Is there a way to specify search and replace range using the current line as a reference? I can specify range using explicit line numbers like

:5,15s/foo/bar/g
<         


        
相关标签:
2条回答
  • 2021-01-30 11:09

    :help :range gives you all the details; you can do quite sophisticated things, e.g. :'a;/pat1/-1.

    For ranges starting from the current line, a neat trick is to start command-line mode by prefixing the : with a count: E.g. 5: turns into :.,.+4.

    Protip: Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor.

    0 讨论(0)
  • 2021-01-30 11:14

    You can use . for the current position, and .+10 for 10 lines down:

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

    Some other useful ranges:

    • % for the whole file.
    • '<,'> for the lines of visually selected block. This might get you more than you want of the visual selection starts or ends in the middle of a line. The whole line is included.
    • 'a,'b from mark a to mark b.
    • .,$ from the current line to the end of the file.
    0 讨论(0)
提交回复
热议问题