I am using vim and I want to replace
&\\ \hline to \\ \hline
Can somebody teach me how to do this?
%s/\&\\/\\/gc didn't work.
this is for latex table.
Inside the search pattern, you don't need to escape & (\& is a special atom for branches), but \ needs to be doubled. In the replacement part, a & would have to be escaped, just like the \ (yes, this is a bit complex, unfortunately). So, this would work:
:%s/&\\\\/\\\\/gc
If you want to assert the following \hline, too, it's simpler to end the matching with \ze (but still assert that the following part is there); this avoids having to duplicate the part that should be kept (or alternatively capture and re-insert it in the replacement):
:%s/&\ze\\\\ \\hline//gc
Related
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.
How do I substitute &&&&&&&&& with &? I have tried :%s/&&&&&&&&&/&/gbut i only get more &.
Because & is special in the replacement part (it means "the whole matched string"), you have to escape it:
:%s/&&&&&&&&&/\&/g
(Note the backslash before & in the replacement part.)
You must escape & in the replacement section. Unescaped, & refers to the whole match.
:%s/&&&&&&&&&/\&/g
You need to escape & with \ in replace string
:%s/&&&&&&&&&/\&/g
In replace string, & has a special meaning and contains matching string.
Therefore in you case, you are replacing nothing but match itself hence no change.
If your intension is to replace multiple & with a single one, then try following
:%s/&\+/\&/g
You need a quantifier in the expression:
:%s/&\+/&/g
not sure if it works like that in vim, it is default regex, the plus sign tells it should capture any combination of one or multiple '&' signs, using &{9} should find exactly nine
I have to search for something like &variable (and replace with &variable.test). I could just do \&variable, but vim also shows me all characters starting with &variable (&variableXXX for instance), which I do not want. I thought this - /\<\&variable\> would work, please let me know where am I going wrong.
The first problem is that \& is not a literal & its a special regex match that matches branches in vim. To match a literal & just use &.
The second problem is that \< matches the beginning of a keyword by looking to see if the next character is a keyword. However & is not normally a keyword so it always fails.
To get around this you can make & a keyword by doing
set iskeyword+=&
Then you can use the regex \<&variable\> to match the things you want.
Relevant help pages :h iskeyword, :h /\< and :h \&
Short answer: use /&variable\>.
You don't want the \ in front of the &. See help \&.
Your \< is in the wrong place -- it belongs after the &, not before, since & is not a keyword character (:help keyword).
In fact, you don't need the \< at all. You could search for &variable\> and you would find &variable but not &variableXXX. You don't have to use \< just because you're using \>.
I want to replace a as using C#. I could not able to achive this using Regex.Replace functions as follos
Regex.Replace(html, "\\"", "\"");
execution this command again produces the original output
Anyone have already faced issue like this,Any help would be of greatly appreciated.
Regards,
Ganesan
first of all "\\""produces a compiler error, since you are just escaping one backslash but not the quote.
you are working with 2 escape mechanisms here, one is from the c# compiler, and another is from the regex interpreter.
Which means:
when you give this C# string as a regex: "\\\"" then after compilation there is a string looking like that \", which is then interpreted by the regex engine, which also uses \ as the escape character. therefor regex will escape ", so your code will replace " with "
so if you now use "\\\\\"", first the c# compiler will make \\" out of that, then the regex engine will make \" out of that (both are using \ as escape character)
now c# has a nice little feature to make such strings easier to write.
if you add an # before your string, \ will no long be the escape character, but now you have to escape " with ""
that means "\\\"" == #"\""" and "\\\\\"" == #"\\"""
so you could write Regex.Replace(html,#"\\""","\"")
which is easier to read then Regex.Replace(html,"\\\\\","\"")
i think i got it right this time :D
In order to replace each occurrence of "&" to "&&" in the range from the current line to 30 more lines on, I issue :.,+30s/\\&/\\&\\&/g but Vim says "Pattern not found". I'm escaping the special character "&" by double backslash "\".
Try not escaping everything - :.,+30s/&/&&/g seems to work for me.
You don't need to escape the ampersand in the first part of a regular expression. It only has special meaning in the second (replace) part.
You are looking for patterns that say "\&" and replace them with patterns that say "\&\&".
The simple way to replace is just:
:.,+30s/&/&&/g
It can also be useful remark that if you need to change the & position for example: _& to &_ the & has to be escaped only in the replace statements of the substitute expression:
:.,+30s/ &/\& /g
^