How to replace ~ (tilde) in vim - vim

I have a string with a ~ in it and using the expression
Example:
hi~how~are~you
:%s/~/ /g
This doesn't seem to work any ideas?

The symbol ~ matches the previous substitute string (see :help /~), so you need to prefix it with a backslash:
:%s/\~/ /g

You just need to escape it with a backslash:
:%s/\~/ /g

:%s/\~//g
Need to use a backslash for the tilde.

In case anyone else copies a tilde from, e.g., microsoft word, you might also need to search for character 8764 / Hex 223c / Octal 21074 (the ascii tilde is 126/Hex 7e/Octal 176). You can enter that by typing <ctrl-V> u 223c (see http://vim.wikia.com/wiki/Entering_special_characters for details on entering character codes)

Related

Find and replace '\' in vim [duplicate]

For instance, if I wanted to a find and replace with strings containing backward or forward slashes, how would this be accomplished in vim?
Examples
Find & Replace is: :%s/foo/bar/g
what if I wanted to find all occurrences of <dog/> and replace it with <cat\>
Same way you escape characters most anywhere else in linuxy programs, with a backslash:
:%s/<dog\/>/<cat\\>
But note that you can select a different delimiter instead:
:%s#<doc/>#<cat\\>#
This saves you all typing all those time-consuming, confusing backslashes in patterns with a ton of slashes.
From the documentation:
Instead of the / which surrounds the pattern and replacement string, you
can use any other single-byte character, but not an alphanumeric character,
\, " or |. This is useful if you want to include a / in the search
pattern or replacement string.
%s:<dog/>:<cat>
You can replace the / delimiters if they become annoying for certain patterns.
Quote them with a backslash. Also, it often helps to use another delimiter besides slash.
:%s#<dog/>#<cat\\>#
or if you have to use slash as the substitute command delimiter
:%s/<dog\/>/<cat\\>/
I was looking for something similar, to search for register values containing the / character (to record a macro). The solution was to search using the ? token instead of the /.
The syntax is:
:%s/<dog\/>/<cat\\>/g
backslash slash backslash star
/(<- the prompt)\/\*
so after you type it looks like
/\/\*

How to replace '&=' with '=&' in Vim?

I tried to replace '&=' with '=&' in Vim, but the command :%s/&=/=&/g
replaced '&=' with '=&='.
Why did that happen ?
What command should I use ?
Just escape the ampersand.
:%s/&=/=\&/g
You need to escape it because & is a special character in the replacement that will be replaced with the entire matched pattern.

Vim Search/replace: what do I need to escape?

I'm trying to search and replace $data['user'] for $data['sessionUser'].
However, no matter what search string I use, I always get a "pattern not found" as the result of it.
So, what would be the correct search string? Do I need to escape any of these characters?
:%s/$data['user']/$data['sessionUser']/g
:%s/\$data\[\'user\'\]/$data['sessionUser']/g
I did not test this, but I guess it should work.
Here's a list of all special search characters you need to escape in Vim: `^$.*[~)+/
There's nothing wrong with with the answers given, but you can do this:
:%s/$data\['\zsuser\ze']/sessionUser/g
\zs and \ze can be used to delimit the part of the match that is affected by the replacement.
You don't need to escape the $ since it's the at the start of the pattern and can't match an EOL here. And you don't need to escape the ] since it doesn't have a matching starting [. However there's certainly no harm in escaping these characters if you can't remember all the rules. See :help pattern.txt for the full details, but don't try to digest it all in one go!
If you want to get fancy, you can do:
:%s/$data\['\zsuser\ze']/session\u&/g
& refers to the entire matched text (delimited by \zs and \ze if present), so it becomes 'user' in this case. The \u when used in a replacement string makes the next character upper-case. I hope this helps.
Search and replace in vim is almost identical to sed, so use the same escapes as you would with that:
:%s/\$data\['user'\]/$data['session']/g
Note that you only really need to escape special characters in the search part (the part between the first set of //s). The only character you need to escape in the replace part is the escape character \ itself (which you're not using here).
The [ char has a meaning in regex. It stands for character ranges. The $ char has a meaning too. It stands for end-line anchor. So you have to escape a lot of things. I suggest you to try a little plugin like this or this one and use a visual search.

How to search and replace an unprintable character

I've a file that was exported from Word and it replaced all quotes with strange unicode characters which aren't correctly displayed in vim.
So now I want those characters to be replaced with quotes, but I don't know how to enter this character in
:%s/???/'/g
The characters look like this: ~U ~R. But of course I can't just mark them with mouse and paste in the command.
You can try setting the encoding type and see if it fixes the visalizations of those characters:
:set encoding=utf-8
then you can use them directly. Alternatively, you can place your cursor on the unprintable character and hit ga, it will show the decimal/hex/octal code of that character, then you can substitute it with:
:%s/\%xYY/substitute/g
where YY is the hex code of the char, if it's multibyte:
:%s/\%uYYYY/substitute/g
for details:
:help character-classes
Note that you can search and match with \%xff or \%uabcd but will be unable to substitute with it.
I usually:
delete the character with: x
undo my change with: u
do the substitute thanks to c_CTRL-R: :%s/^R"/'/g
you can also filter it by using the tr command
for example replacing the hex a0 which stems from MacOs cut-and-paste
can be replaced with a whitespace as follows (\240 being the octal representation of hex a0)
:.,$!tr "\240" " "

Can you write unescaped search terms in the Vim command line?

When I do a search and replace on a yanked line that has characters that require escaping it's really annoying to have to go back through the command line string and escape all the special characters.
Is there a way to use a literal (raw) string modifer like in python: str = r'\unescaped\'?
Use the \V modifier:
:%s/\V$foo//g
See also :help /magic
I just thought of doing this instead: :%s;\foo\;bar;g. This solves the escape the backslash problem Manni points out.

Resources