VI is a very powerful tool for editing text files. a little tedious and before I had a bit of a obsession with it, but as I'm using it I'm getting to love it.
To search for a string (From the cursor position on):
/String to searchSearch from the end, to do this we change / to ?:
?String to searchWe search and replace:
:s/String to search/Replace/Explanation:
s/” means “search”, or “search”, and what appears after the bar is the string to search for.
In the previous step, only the first string that matched would change, to do so in all the matches of a line we added g in the end
:s/String to search/replace/gs/” means “search”, or “search”, and what appears after the bar is the string to search for.
“/g” to finish means “global” (the opposite would be not to make the substitution to everything found, or to do it “interactively”).
To carry out the search and replace throughout the file we will have to add % before the whole of the expression:
:%s/String to search/Replace/In this case it would be done for all the lines of the text, but only the first instance of each line, so to do it for all the occurrences in the text we must complete the expression with a g in the end:
:%s/find/replace/gWe can perform the substitution for a set of lines. For example, to make the substitution between lines 3 and 10 we would do the following:
:3.10s/find/replace/We would also add the g to perform the substitution for all occurrences of the line:
:3.10s/find/replace/gInformation sought in:
http://systemadmin.es/2009/06/uso-de-vi-buscar-y-reemplazar
http://enriqueplace.blogspot.com/2005/09/vim-buscar-y-reemplazar-una-cadena-de.html
Comments