Wikia

Vim Tips Wiki

Changes: Verbose replace without 'c' flag

Edit

Back to page

(Initial commit)
 
(Insert TipProposed template + minor manual clean)
 
Line 1: Line 1:
Some times I want to replace something in a file, and see if my pattern did not match things I don't want to replace. The vim solution is to use 'c' flag for the substitute command, though sometimes I don't want to go through every substitution. Here is my simple solution, which prints every line which matches the pattern, then makes the substitution, and then prints it again, so you can compare if the changes are ok.
+
{{TipProposed
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=October 21, 2012
  +
|complexity=basic
  +
|author=Marcin Szamotulski
  +
|version=7.0
  +
|subpage=/201210
  +
|category1=
  +
|category2=
  +
}}
  +
Sometimes I want to replace something in a file, and see if my pattern did not match things I don't want to replace. The Vim solution is to use 'c' flag for the substitute command, though sometimes I don't want to go through every substitution. Here is my simple solution, which prints every line which matches the pattern, then makes the substitution, and then prints it again, so you can compare if the changes are ok.
  +
 
==Solution==
 
==Solution==
 
The solution is to use the global command:
 
The solution is to use the global command:
Line 5: Line 5:
 
:g/PAT/#|s/PAT/REP/g|#
 
:g/PAT/#|s/PAT/REP/g|#
 
</pre>
 
</pre>
 
   
 
==Comments==
 
==Comments==
[[Replace]]
 

Latest revision as of 08:01, January 12, 2013

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created October 21, 2012 · complexity basic · author Marcin Szamotulski · version 7.0

Sometimes I want to replace something in a file, and see if my pattern did not match things I don't want to replace. The Vim solution is to use 'c' flag for the substitute command, though sometimes I don't want to go through every substitution. Here is my simple solution, which prints every line which matches the pattern, then makes the substitution, and then prints it again, so you can compare if the changes are ok.

SolutionEdit

The solution is to use the global command:

:g/PAT/#|s/PAT/REP/g|#

CommentsEdit

Around Wikia's network

Random Wiki