Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - Step through search and replace results

Tags:

vim

Is it possible to step through the results of a search and replace to ensure that no unwanted modifications were made to your code in a boldly-executed, entire-file search and replace?

Vim throws up the status message:

"*X* substitutions on *Y* lines"

and I'm curious if there's a key or command to step through each of those substitutions and do a quick check to make sure you haven't shot yourself in the foot with an unexpected string match. I am new to Vim.

like image 648
SheffDoinWork Avatar asked Mar 19 '14 16:03

SheffDoinWork


2 Answers

From http://vim.wikia.com/wiki/Search_and_replace,

:%s/foo/bar/gc
    Change each 'foo' to 'bar', but ask for confirmation first.
like image 139
emsworth Avatar answered Sep 21 '22 19:09

emsworth


This is how you should have found that info yourself, without Google, the Vim wiki or SO:

  1. Look for help on the :substitute command:

    :help :s
    
  2. Read the paragraph and notice the part where it talks about optional [flags]:

    :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
        [...
        skipped lines
        ...]
        See |:s_flags| for [flags].
    
  3. Hit <C-]> on the highlighted :s_flags.

  4. Hooooo…

like image 40
romainl Avatar answered Sep 20 '22 19:09

romainl