How to replace "%" inside vim? \% doesn't work - vim

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+/+//+

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
/\/\*

Putting "\n" in my .c or .cpp file using vim

I'm using vim to make my programs in c/c++ and I would like to know how can I put "\n" (which represents a newline) in my code or use "%" using :%s.
For instance, sometimes I forget to put "%" in front of "d" or "f" in many lines or forget to put "\n" in some printf() calls.
printf("This is my d code.", x);
But the following command does not work, it puts a space in place of "\n"!
:%s/\<code.\>/code.\n/gc
or
:%s/\<d\>/%d/gc
How can I do what I want?
The :help s/\n has the answer:
\n insert a <NL> (<NUL> in the file)
(does NOT break the line) *s/\n*
You'll also find the solution there: to insert a literal backslash, escape it \\ by doubling; to split the line, a \r has to be used. Yes, this is inconsistent, and it works differently in similar tools like sed, but that's unfortunately how it is.
Notes
The \n doesn't insert a space, but the special <NL> character, which usually is shown as ^#.
The \<code.\> isn't right; to match a literal period, you have to escape it: \.. Else, it matches any character. Likewise, the . usually isn't a keyword character, so the \> boundary wouldn't match.
You don't need to repeat the match text in the replacement, you can use & for it. Also read up on capture groups (:help /\() and the submatch references :help s/\1. This is a better way:
:%s/\<code\./&\\n/gc
(I don't see a problem with the second substitution.)
You want to insert the two-character sequence \n, not a literal newline (the latter would create a syntax error).
A sample line to be changed is:
printf("This is my d code.", x);
One problem with your attempt:
:%s/\<code.\>/code.\n/gc
is that there is no word boundary between the . and the " following the word code. The other problem is that \ in the target is used to escape special characters (for example you can refer to a / character as \/), so the \ must itself be escaped.
This should do the job:
:%s/\<code\."/code.\\n"/gc
A more general solution might be:
:g/printf/s/"/\\n"/egc
which offers to replace " by \n" on each line that contains printf -- but that will miss any printf calls that span more than one line.
As for replacing the d by %d, the command you have in your question:
:%s/\<d\>/%d/gc
is correct.

Find and replace a string separated by delimiter

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.

How to replace ~ (tilde) in 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)

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" " "

Resources