Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include forward slash in vi search & replace

I have a file that contains the string usrbin. I want to search for usrbin and replace it with /usr/bin/.

I tried :%s/usrbin/usr/bin/g, but it's showing error E488: Trailing characters.

How do I include a forward slash in a search and replace?

like image 546
user1578447 Avatar asked Aug 06 '12 06:08

user1578447


People also ask

How do I find backslash in vi?

To escape a special character, precede it with a backslash ( \ ). For example, to search for the string “anything?” type /anything\? and press Return. You can use these special characters as commands to the search function.

How do I find slashes in Vim?

Basic Search To go back to normal mode from any other mode, just press the Esc key. Vim allows you to quickly find text using the / (forward slash) and ? (question mark) commands. It is important to note that the search command looks for the pattern as a string, not a whole word.

How do I find a slash?

Slash to search. Jump to search box by pressing forward slash. This extension lets you jump to the search box on websites by pressing the forward slash '/' key. It also lets you choose to jump to the search box automatically when a website loads; an option ideal for YouTube and Jumia.

How do I change to global in vi editor?

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.


1 Answers

Here are two ways:

  • escape the / which is the default substitute separator: :s/usrbin/\/usr\/bin
  • use another substitute separator, e.g., using the hash # character: :s#usrbin#/usr/bin. Note that there are characters that you can't use as a separator: ", \, |

You can review this in the help subsystem using :h pattern-delimiter

like image 62
pb2q Avatar answered Nov 04 '22 04:11

pb2q