How to replace ~ (tilde) in vim

2020-02-27 18:02发布

问题:

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?

回答1:

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

:%s/\~/ /g


回答2:

You just need to escape it with a backslash:

:%s/\~/ /g


回答3:

:%s/\~//g

Need to use a backslash for the tilde.



回答4:

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)



标签: vim replace