The command :%s/foo/bar/gc is to replace all "foo" by "bar" in the global field with confirmation. There are many "foo"s in my file within first 50 lines not to be changed. How can make this command effective only for lines 51 to last line?


2 Answers 2


The find and replace command has a range option. In this case you are performing this action against the entire document %. You can also select a more specific range. So lets say you want to swap lines 51 through 57


If you want to go to end of the document use $


Per :h range

Line numbers may be specified with:             :range E14 {address}
        {number}        an absolute line number
        .               the current line                          :.
        $               the last line in the file                 :$
        %               equal to 1,$ (the entire file)            :%
        't              position of mark t (lowercase)            :'
        'T              position of mark T (uppercase); when the mark is in
                        another file it cannot be used in a range
        /{pattern}[/]   the next line where {pattern} matches     :/
        ?{pattern}[?]   the previous line where {pattern} matches :?
        \/              the next line where the previously used search
                        pattern matches
        \?              the previous line where the previously used search
                        pattern matches
        \&              the next line where the previously used substitute
                        pattern matches        

As an alternative, if you don't know the line number you can use :global to find the pattern that indicates the point where you want to start substituting. E.g.:

:g/regex pattern/,$s/foo/bar/gc

The ,$ part extends the range until the end of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.