3

I have this sample line in the buffer:

export PS1="\[${Cyan}\]$(((SHLVL>1)) && echo "${SHLVL}\[${IBlack}\].")\[${IGreen}\]\u\[${IBlack}\]@\[${Purple}\]\h\[${IYellow}\] \w \$ \[${Color_Off}\]"

and i want to delete all \[ so while the cursor is on the line i write :s/\\[//g and get the error

E486: Pattern not found: \\[//g

What do i do wrong? Somehow vim thinks that //g is part of the pattern?

When i search with /\\[ i get proper results.

  • 2
    I'm not too sure but I /think/ this is a bug. I also could be wrong so I'm not posting this as an answer. However, to circumvent this, you can search [ using /\[ and then use :s///g Here, vim will use the old search term in the blank pattern – akshay Mar 13 '15 at 19:18
  • I suspect that \[ has some special meaning which i do not understand and can't find it on google. – Kossak Mar 13 '15 at 19:19
  • 1
    You can use the very nomagic mode like so: :s/\V\\[//g (See :help \V) – akshay Mar 13 '15 at 19:22
  • 1
    I tried it, and it works if you escape the [ like so: :s/\\\[//g – EvergreenTree Mar 13 '15 at 19:23
  • 1
    @Akshay: I think it's a bug, too. Posted here on vim_dev. – Ingo Karkat Mar 13 '15 at 20:55
4

I found the reason: [ is special character so vim treats next characters as part of the set ([...]) although it doesn't have the closing bracket. The proper replace should be:

:s/\\\[//g

(One backslash more)

3

The error gives it away: Vim interprets the whole \\[//g as the pattern. It doesn't recognize the / separator because the [ starts an (unclosed) collection. You either need to escape (via another backslash: :s/\\\[//g, or use very nomagic mode via \V: :s/\V\\[//g.

  • Just a small comment, \V means very no magic – akshay Mar 13 '15 at 19:40
  • @Akshay: Right, corrected! – Ingo Karkat Mar 13 '15 at 19:50
0

If this would be slash, you could use another delimiter, however backslash (\) is metacharacter which escapes any separator, so there is no way to omit two backslashes for backslash it-self. The same with [.

However using different delimiter can still improve the readability:

:s,\\\[,,g
:s@\\\[@@g
:s#\\\[##g
:s|\\\[||g

I've tried to test with <Bslash>, but it seems substitute doesn't accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.