Find and replace a string separated by delimiter - linux

How can i find and replace a string like "aa:bb:cc:dd" with "ring12" when using vi in linux? For normal strings without delimiter, i know it as
%s/randomstring/ring12/g
But how to deal with string having ':' symbol? I know i should use backslash '\' but don't know the syntax.

actually it doesn't require a \ ... you can simply use something like this:
<ESC>:%s,aa:bb:cc:dd,ring12,g
Where <ESC> is the escape key to put vi in command mode.

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 "%" inside vim? \% doesn't work

I have c format text like this:
"%d %d"
I wish to change it into text like "%l %l"
I tried in vim command mode like:
:%s:\%d:\%l:g
It doesn't work. Then I tried:
:%s:%%d:%%l:g
Doesn't work either. How to fix it?
You don't need to escape the % character. Either of these work:
:%s/%d/%l/g
:%s:%d:%l:g
You're not limited to just / or : as delineation characters though. From vim's help:
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. Example: >
:s+/+//+

Execute vim substitute command that includes '/' characters

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

vim string substitution

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

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