As the title says. I want to replace, say "tabs", with the ampersand sign (&).
I use:
:%s/\t/&/g
and of course it doesn't work. I use vim on mac os x (if this makes a difference). Thanks!
Are you sure it's the ampersand that's the problem? I got more complaints about the tab. Don't forget to escape it.
:%s/\t/\&/g
The problem with the ampersand is that it is a regular expression metacharacter for backreferences. The &
character in the replacement part of the regular expression means "everything that was matched in the match-part of the expression".
So, for example if you had a file containing the following line:
The quick brown fox jumps over the lazy dog.
Executing a vi command like :s/jumps/a&b/
would result in the following line:
The quick brown fox ajumpsb over the lazy dog.
You can do the same thing using :s/\(jumps\)/a\1b/
but that's a lot more typing.
Why is & useful, other than just an alias for e.g. \1
in a simple match expression? Well, you can also do things like this: :s/lazy \(dog\|cat\)/"&" is now "stupid \1"/
:
The quick brown fox jumps over the "lazy dog" is now "stupid dog".
The expression would get very ugly if &
were unavailable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With