How can I do a find-replace for a Backslash (\) character in zshell? - string

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.

Related

String commands in the file path

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.

TFS 2017 - Variable substitution with double backslash in shell script

I'm trying to substitute a variable with double backslash using shell script task, my environment variable is configured to use double backslash.
When is substituted in bash script, the TFS puts only one backslash.
Variable:
Substitution in script, I'm using double quotes because of white spaces in connection string:
Log of execution:
I tried everything, put more backslashs, one quote around, but nothing works.
I appreciate a help.
Thanks
You need type \\\\\\\\ in your variable:
CONNECTION_STRING: Data Source=192.168.2.227\\\\\\\\COMMERCE
Yes, I solved it!!!
I just put the backslashes inside double quotes

How do I create files with special characters in Linux?

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.

Searching for \[ in VIM

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.

vim :gsearch (greplace plugin) escaping characters

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.

Resources