Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search and replace a literal ampersand "&" in vim

Tags:

vim

I'm trying to search and replace " &" (space character and & character, without quotation marks) with "& " (& followed by space).

I tried :%s/ &/& /g as well as /\s&/ and /\ &/ but none of these seem to pick up the first space. Is there something I'm missing?

like image 943
GBirzu Avatar asked Sep 06 '25 17:09

GBirzu


1 Answers

In vim regular expressions, '&' is a special character that means "the whole matched pattern", so "& " is expanding to " & ". To fix this, escape the &:

:%s/ &/\& /g

like image 160
Alcamtar Avatar answered Sep 10 '25 20:09

Alcamtar