Vim’s Replace Tips – Mosky’s Notes
Report Number of Matches

:%s/word//ng

  • g: do multiple times in a line (usually you want it).

Replace After Confirm

:%s/word//c

[c]     Confirm each substitution.  Vim highlights the matching string (with
        hl-IncSearch).  You can type:                           :s_c
            'y'     to substitute this match
            'l'     to substitute this match and then quit ("last")
            'n'     to skip this match
            <Esc>   to quit substituting
            'a'     to substitute this and all remaining matches
            'q'     to quit substituting
            CTRL-E  to scroll the screen up
            CTRL-Y  to scroll the screen down


Replace With Matched

:%s/app/&le/       " app -> apple, or
:%s/app/\0le/      " or
:%s/\(app\)/\1le/

Replace With Register

/something
:%s/<C-R>/

Replace With Different Delimiters

:%s'/home/mosky'/home/notmosky'

Replace From Current Line

:.,$s/something//

Replace With \n and \r

:%s/\n\+/\r/  " Replace newlines with a newline.

When magic (default), whose special chars are \ * ^ $ . [] ~, the + is a normal char, and the \ makes + have the special meaning. And note that \n is end-of-line and \r is <CR> in Vim.

Replace Without Magic