Replace & not at the end of the line and do not precede \ with \ & in Vim

I need to replace &with \&when &it is not at the end of the line and is not preceded by it \. I can successfully find &when it is not preceded \, but I cannot exclude those that are at the end of the line.

line 1 &
line 2
line 3 another &
line 4 & is at the middle

In four lines, I want to replace &with \&in line 4.

How can i do this?

Here is what I can do so far:

/\(\\\)\@<!&

Find &if it is not preceded by \. By adding a negative lookahead \@!$that uses:

/\(\\\)\@<!&\@!$

I should get those &that are not at the end of the line, but I cannot.

+3
source share
2

- ?

:%s/\(.\|^\)&\(.\)/\1\\\&\2/g

, &, , .

+1

, , , , \@! . ( ), , (. :help /\@!).

,

:%s/\\\@<!&\_$\@!/\\\&/g

\_$ $ one, \|, \) \n (. :help /$ :help /\_$).

. , , :

:%s/\\\@<!&./\\&/g

, &, \& string, , (. :help s/\&).

+1

Source: https://habr.com/ru/post/1772708/


All Articles