I have a TeX file that contains lines like
\[ De = 0 \]
Now my boss wants equation numbers so I want them to be like
\begin{equation}De = 0\end{equation}
I tried the following command by escaping the backslash with a backslash.
:%s#\\[#\\begin{equation}#gc
I think this should work but I am getting pattern not found error.
I have looked at
How to include forward slash in vi search & replace
and
http://vim.wikia.com/wiki/Search_and_replace
Can somebody tell what am I doing wrong?
You have to escape both characters: :%s_\\\[ _\\begin{equation}_g.
EDIT: Since you also asked for an explanation:
Why escape the \?
Vim supports different pattern matching styles (see :help /magic), but escape characters have to be escaped in all of them.
Why escape the [?
[] in patterns are used as collections. Thus you have to escape the [ to match it literally.
Related
I'm trying to use a ZShell function to replace forward- and backslashes with a hyphen. So that "Hello/Every\Person" becomes "Hello-Every-Person". I can do it for the forward slash using
arg=${arg:gs/\//-}
but when I try the same syntax for the backslash it fails. Running
arg=${arg:gs/\//-}
arg=${arg:gs/\\/-}
produces "Hello-EveryPerson". It strips out the backslash, but doesn't do the replacement. I was expecting the normal rules of escaping characters to apply, so the double backslash would resolve to a character.
Can anyone tell me what I'm missing?
Works in zsh version 5.8 exactly as you expected.
I wanted to write the file path into a variable so i used the str to do that and got that:
\f inside of the path
and that happens often so how do i solve this issue?
Replace all your \ with \\.
From the Python docs:
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?
You need to escape special characters with the backslash symbol (\).
This command will create a file named "\?$*'KwaMe'*$?\":
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, \*, to prevent filename expansion.
I'd like to replace all line containining "CreateTime=xxxxx" with "CreateTime=2012-01-04 00:00". May I know how should I do it with vim?
[m18]
Attendees=38230,92242,97553
Duration=2
CreateTime=2012-01-09 22:00
[m20]
Attendees=52000,50521,34025
Duration=2
CreateTime=2012-01-09 00:00
[m22]
Attendees=95892,23689
Duration=2
CreateTime=2012-01-08 17:00
You can use the global substitute operator for this.
:%s/CreateTime=.*$/CreateTime=2012-01-04 00:00/g
You can read the help for the s command from within Vim using:
:help :s
You can read about patterns with :help pattern-overview.
As requested, a bit more about the regular expression match (CreateTime=.*$):
CreateTime= # this part is just a string
. # "." matches any character
* # "*" modifies the "." to mean "0 or more" of any character
$ # "$" means "end of line"
Taken together, it matches CreateTime= followed by any series of characters, consuming the rest of the line.
I am using the greplace plugin for vim and am not sure how to escape brackets in a search.
I want to search for cookies[:parent] and have tried:
:Gsearch cookies[:parent] # returns nothing
:Gsearch cookies\[:parent\] # returns nothing
How should I be doing this?
Thanks
Try
Gsearch cookies\\\[:parent\\\]
or
Gsearch 'cookies\[:parent\]'
. If I understood correctly, shell invoked by :grep! invoked by :Gsearch gets string grep -n cookies\[:parent\] /dev/null (assuming grepprg option has default value) and thus your escapes are interpreted by shell that thinks they are for escaping [ in order to prevent glob expansion. But after globbing done by shell grep takes argument as a pattern, so you need to escape it for grep also and it is why I have three backslashes here: two are to make grep get a backslash and third to prevent glob expansion.
:Gsearch cookies\\\[:parent] works for me.
Remember that :Gsearch requires a file mask in addition to the pattern, so in reality, you'd want to type something like :Gsearch \\\[:parent] *.php or whatever, to specify which files you want to have searched.
:Gsearch cookies\[:parent]
[ is the start of a character class, so needs to be escaped. The ] isn't particularly special so doesn't need to be escaped.