I have a java file in which I wrote /t, instead of \t, the proper notation of the tab escape function. I want to use vim’s substitution feature (:s) but when I do something like this:
:%s//t/\t/g
I get the error message
E488: Trailing characters
How can I write the command such that I can execute the substitution? Thank you.
Use backslash to escape the characters. So:
:%s/\/t/\\t\g
You can also use a different delimiter like:
:%s#/t#\\t#g
Use colons instead of a slash to avoid confusions and errors if what you want to look-up/replace has slashes. This way VIM will be able to parse the sentence correctly. Then you need to scape the back-slash otherwise VIM will replace /t with tabulations
:%s:/t:\\t:g
Escape the forward slash in your search pattern:
:%s/\/t/\\t/g
or use another separator:
:%s;/t;\\t;g
Related
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
/\/\*
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.
I am trying to run this command in vi
:s/href="\//href="http:\/\/website.com\/folder\/subfolder\//g
but got this error E486: Pattern not found: href="\/
What am i doing wrong?
That error means pretty much what it says. vi didn't find any pattern href="/ (ignoring escapes) in your file.
Sometimes it's easier to use something besides / for the search delimiter if your search has a lot of slashes, so you don't need to escape them all. Try replacing the / delimiter with # instead, like this:
s#href="/#href="http://website.com/folder/subfolder/#g
Then maybe you can more easily see what's wrong with your pattern:
becouse there are many '/' chars, try use another delimiter, ex ',':
:s,some/pattern/with/slashes,new/string,g
On another note. That substition worked for me. Just copied and pasted. Are you on the same line that you are trying to perform the substitution on? the 'g' is meant globally on the line you are on. If you need to perform the search and replace on the file the use :%s/
I want to substitute all cygdrive/e with cygdrive/d using vim. But I can't get the matching pattern correctly. Here is my command:
s/cygdrive\/e/cygdrive\/d/g
it doesn't work. Can anybody show me what is wrong?
Thanks,
vim allows you to specify the delimiter.. (First character after s is the delimiter)
s/cygdrive\/e/cygdrive\/d/g
using line range argument .. and # as delimiter
ESC:
:1,$ s#/cygdrive/e#/cygdrive/d#g
Your search pattern and replacement string look fine.
Make sure you are in ex mode when you try it.
So press ESC, then : and then
%s/cygdrive\/e/cygdrive\/d/g
But if you want all he replacements in just the current line you can do:
s/cygdrive\/e/cygdrive\/d/g
You have to escape the special character
like this
s/cygdrive\/e/cygdrive\/d/g
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.