I editing a latex file into html and I want to change all \\ with a <br /> -tag.
:%s#\\#<br />#g
Vim uses the first character after the :%s as delimiter so that not a problem.
But I keep getting all the single \ in my search just as as \\. I'm guessing that the first \ is mapped to something but I don't no what or how to fix it.
And if I instead search do
:%s#\\\#<br />#g
I get E59 : invalid character after \# and E476 Invalid command.
Are you trying to replace all occurrences of two backslashes with <br />? If so, you need to escape both backslashes:
:%s#\\\\#<br />#g
A backslash is interpreted as an escape character that pairs up with the next character to indicate something special. For example, \t is used to indicate a tab character. To specify a backslash itself, you need two backslashes, so if you want two backslashes consecutively, you need to enter four.
Related
Hi i am trying to replace a string with special character at the end with new string. For Example, I want to replace
qwerty_CRS_abc\
to
qwerty_CRS_abc
I tried with this:
:%s/qwerty_CRS_abc\/qwerty_CRS_abc/g
but I'm getting this error:
Pattern not found: padring_CRS_CAN\/padring_CRS_CAN\g
Basically, I just want to remove that backslash in whole file. It should be just
qwerty_CRS_abc
Use:
:%s/qwerty_CRS_abc\\/qwerty_CRS_abc/g
Certain characters such as /&!.^*$\? carry a special significance to the search process and must be escaped using the \ character when they are used in a search. Hence the \\ used to escape the backslash in your example.
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 saw this in a Python 3 tutorial about how to download a file and this is what it kinda looks like.
from urllib import request
import requests
goog="http://realchart.finance.yahoo.com/table.csvs=GOOG&d=8&e=7&f=2016&g=d&a=7&b=19&c=2004&ignore=.csv"
rp=request.urlopen(goog)
s=rp.read()
cp=str(s)
m=cp.split('\\n')
dest='goog.csv'
fw=open(dest,'w')
for c in m:
fw.write(c+ '\n')
fw.close()
fr=open('goog.csv','r')
k=fr.read()
print(k)
Why was this used?
split('\\n')
Its true that the code only works properly when you use the double backslashes but why?
The backslash is a special character inside strings, its purpose is to introduce special characters into the strings, special characters that can't otherwise be written on a keyboard in a natural way, if at all. The most common being the newline '\n'.
However, since the backslash is special, how do one make a string contain an actual backslash? Simple: Use the backslash to escape itself! A double-backslash will be translated into a literal backslash.
In the context of this question, the text being searched contains a literal backslash, so to find this literal backslash one must use the double backslash.
<button onclick='window.alert("\n")'>alert not escaped</button>
<button onclick='window.alert("\\n")'>alert escaped</button>
In a string a single backslash is a so-called 'escape' character. This is used to include special characters like tab (\t) or a new line (\n).
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.
I have 100k lines file and some lines has unescaped apostrophe, like:
""Luis" number 4"
I want:
"\"Luis\" number 4"
so how can I find all lines having more than 2 " character. is it possible to do it easly in vim ? otherwise it is for simple script task.
If you just want to find such lines you can search as follows:
/.*".*".*".*
where .* will match zero or more characters and " is the literal quote you're looking for.
The first and last .* patterns aren't strictly necessary, but they make sure the whole line is highlighted when a search is found.
You can try
:%s/""\([^"]*\)"\(.*\)"/"\\"\1\\"\2"/
%s/\v"(.*)"(.*)"(.*)"/"\1\\"\2\\"\3"/
Breaking this down:
%s: Substitute on every line
\v: Very magic (don't have to escape all those parens in regex)
"(.*)"(.*)"(.*)": Anything in quotes anywhere inside anything in quotes
And replace with a quote, everything up to the opening inner quote (\1), an escaped quote, everything inside the inner quotes (\2), another escaped quote, then the rest (\3) and a close quote.