Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/ in vi Search and replace?

Tags:

text

replace

vim

in vi, search and replace, how do you escape a '/' (forward slash) so that it is correct. Say in a path.

like: /Users/tom/documents/pdfs/

:%s//Users/tom/documents/pdfs//<new text>/g  --FAILS (obviously)  :%s/\/Users/tom/documents/pdfs\//<new text>/g -- FAILS with a trailing error  :%s/'/Users/tom/documents/pdfs/'/<new text>/g -- FAILS with a trailing error 

What am I missing?

like image 414
JT. Avatar asked Nov 06 '09 00:11

JT.


People also ask

How do you do search replace in vi?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .

How do I search and replace globally in vi?

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.

How do I find and replace special characters in vi?

You can use all the special matching characters for searches in search-and-replace. Then press the Return key. Then and press Return. You can modify this command to halt the search and make vi query whether you want to make the replacement in each instance.

What is the command for search and replace?

You can also open the basic Find and Replace pane with the keyboard shortcut CONTROL + H.


1 Answers

Alternatively you can do :%s,foo/bar/baz,foo/bar/boz,g - I almost never use slashes because of the escaping confusion.

like image 108
meder omuraliev Avatar answered Sep 22 '22 20:09

meder omuraliev