Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find and replace a String in vi editor on linux?

i am trying to replace String with new String in Linux vi editor

:s/totel_email_count/total_email_count/g

but getting following error.

E486: Pattern not found: totel_email_count
like image 811
Girdhar Singh Rathore Avatar asked Aug 14 '15 07:08

Girdhar Singh Rathore


People also ask

How will you search and replace text in vi?

To search and replace in vi::%s/ plus the text to find, another /, followed by the replacement text, as in Figure 4.15.

How do I find and replace a string in Linux?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

How do you do a search and replace string in Vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.

How do I replace a word in vi editor globally?

The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string . The global ( g ) flag at the end of the command tells vi to continue searching for other occurrences of search_string . To confirm each replacement, add the confirm ( c ) flag after the global flag.


2 Answers

I'd guess the reason you're getting that error message is because you intend to replace the string on all lines, not just the current one. In order to search all lines, add a % to your command:

:%s/totel_email_count/total_email_count/g
like image 60
user156213 Avatar answered Sep 20 '22 13:09

user156213


To find and replace with vi editor type following:-

Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.

:%s/foo/bar/

for example:-

:%s/old_string/new_string/

like image 36
Sukhjinder Singh Avatar answered Sep 17 '22 13:09

Sukhjinder Singh