Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

Find and replace in VI

Leer en espanol
Find and replace in VI

Table of contents

for bu ===

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):

CODE
text
/String to search

Search from the end, to do this we change / to ?:

CODE
text
?String to search

We search and replace:

CODE
text
: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

CODE
text
:s/String to search/replace/g

s/” 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:

CODE
text
:%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:

CODE
text
:%s/find/replace/g

We 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:

CODE
text
:3.10s/find/replace/

We would also add the g to perform the substitution for all occurrences of the line:

CODE
text
:3.10s/find/replace/g

Information 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

http://bulma.net/body.phtml?nIdNoticia=418

Comments