/ in vi Search and replace?

前端 未结 6 555
耶瑟儿~
耶瑟儿~ 2021-01-30 08:02

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/to         


        
相关标签:
6条回答
  • 2021-01-30 08:44

    You need to escape the forward slashes internally, too.

    :%s/\/Users\/tom\/documents\/pdfs\//<new text>/g
    
    0 讨论(0)
  • 2021-01-30 09:00

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

    0 讨论(0)
  • 2021-01-30 09:05

    You can use ? to search

    In case of searching pattern in a register, and the pattern contains a '/' character, you can simply use ? command instead of / command from normal mode to start pattern matching. In such case, no more escape required for '/' char. (however you need to escape '?' char now)

    ? will search in the opposite direction of /, so if you don't mind the search direction, and your search pattern doesn't contains '?' char.

    In addition, check the escape() script if you want more.

    0 讨论(0)
  • 2021-01-30 09:06

    As Sarah suggested, you need to escape ALL forward slashes.

    You could instead use another character besides forward-slash as the delimiter. This is handy if your search string has a lot of slashes in it.

    :%s#/Users/tom/documents/pdfs/#<new test>#g
    

    This works perfectly in vim. I'm not 100% sure about vanilla vi.

    0 讨论(0)
  • 2021-01-30 09:06

    I know this question is several years old, but for others who may land upon this one searching for an easier solution, in 2014, you can substitute the "/" delimiter for something else like "!", as long as you do it in front, middle, and back, like this:

    :%s!foo/bar/baz!foo/bar/boz!g
    

    Very simiar to Meder's answer ... But, I find that the exclamation is a lot easier to view as a separator. And I just wanted to confirm that this method still works in the current version of VIM, which I am using in Mac OSX Mavericks.

    0 讨论(0)
  • 2021-01-30 09:09

    Windows uses backslash for directories and Linux uses forward slash for directories. Vim is a text editor that works for operating-systems. Since both os have different directory path interpretation regarding how slashes are used, Vim must then need a way to interpret Windows twisted method.

    LINUXFORWARD vs. WINDOWSBACK (DIRECTORY SLASHES)

    • The slashes are literally reversed in Windows and easily stated for Linux
      • Windows:
      • C:\Program Files (x86)\Microsoft OneDrive\
      • Linux:
      • /usr/bin

    SOLUTION

    I'm only able to state the struggle for Vim's Find & Replace on Windows as i'm not on a Linux pc.

    • Fix is to double backslash
      • GOOD:
      • :%s/c:\\Program Files (x86)\\Microsoft OneDrive\\/annoyancereplaced/g
      • BAD:
      • :%s/c:\Program Files (x86)\Microsoft OneDrive\/unabletoreplaceannoyance/g
    NOTE
    • If working with network paths where Windows uses two slashes \\ then that means for every backslash there needs to be another backslash as it's always kept even
      • e.g. network path: \\Foo\Bar\
      • %s/C:\\Program Files (x86)\\foo\\bar/\\\\Foo\\Bar\\
      • notice the \\\\
    0 讨论(0)
提交回复
热议问题