Must I escape every '\' to realize vim substitution of file path? [duplicate] - vim

This question already has answers here:
Execute vim substitute command that includes '/' characters
(3 answers)
Closed 8 years ago.
I need to substitute a long path to a brief new pattern. I feel like escape every \ a dumb idea, it looks like:
:%s/\/my\/very\/very\/dumb\/long\/path\//new_pattern/gc
Is there a smarter way to do that?

Must I escape every '\' to realize vim substitution of file path?
No, you don't.
Is there a smarter way to do that?
Yes. You can use a different character after %s.
:%s+/my/very/very/dumb/long/path+new_pattern+gc
From :help :s
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
/\/\*

How to escape backslashes and forward slashes in VIM find/search? [duplicate]

This question already has answers here:
How does one escape backslashes and forward slashes in VIM find/search?
(6 answers)
Closed 2 years ago.
for example, I want to find and replace, say my original string is "//hello" to "hello".
Actually I'm trying to uncomment lines which I've commented before.
substitution
if your pattern contains slashes /, e.g. //hello you can of course escape them in s/pat/repl/ command. However, better to pick some another delimiter for your s command:
:s#//hello#whatever#g
In this way, the command is easier to read and understand.
Search
Say If you want to search //hello, you can try a backward search with ?, then you don't have to escape the slashes.
Did you try this:
%s/\/\/hello/hello

Execute vim substitute command that includes '/' characters

I have a java file in which I wrote /t, instead of \t, the proper notation of the tab escape function. I want to use vim’s substitution feature (:s) but when I do something like this:
:%s//t/\t/g
I get the error message
E488: Trailing characters
How can I write the command such that I can execute the substitution? Thank you.
Use backslash to escape the characters. So:
:%s/\/t/\\t\g
You can also use a different delimiter like:
:%s#/t#\\t#g
Use colons instead of a slash to avoid confusions and errors if what you want to look-up/replace has slashes. This way VIM will be able to parse the sentence correctly. Then you need to scape the back-slash otherwise VIM will replace /t with tabulations
:%s:/t:\\t:g
Escape the forward slash in your search pattern:
:%s/\/t/\\t/g
or use another separator:
:%s;/t;\\t;g

Bash scripting: Diference between "$(command)" and `command` [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Command substitution: backticks or dollar sign / paren enclosed?
I want know the difference between use
var="$(command)"
and
var=`command`
in bash scripting, aparently are two ways to get the same result, but is possible that exist some diferences.
The first is better, as you can nest the command substitutions and it doesn't become awkward.
Further Reading.
From bash man page:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘`’, or ‘\’. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
(more interesting things there; do read the whole thing :) )

Vim Search/replace: what do I need to escape?

I'm trying to search and replace $data['user'] for $data['sessionUser'].
However, no matter what search string I use, I always get a "pattern not found" as the result of it.
So, what would be the correct search string? Do I need to escape any of these characters?
:%s/$data['user']/$data['sessionUser']/g
:%s/\$data\[\'user\'\]/$data['sessionUser']/g
I did not test this, but I guess it should work.
Here's a list of all special search characters you need to escape in Vim: `^$.*[~)+/
There's nothing wrong with with the answers given, but you can do this:
:%s/$data\['\zsuser\ze']/sessionUser/g
\zs and \ze can be used to delimit the part of the match that is affected by the replacement.
You don't need to escape the $ since it's the at the start of the pattern and can't match an EOL here. And you don't need to escape the ] since it doesn't have a matching starting [. However there's certainly no harm in escaping these characters if you can't remember all the rules. See :help pattern.txt for the full details, but don't try to digest it all in one go!
If you want to get fancy, you can do:
:%s/$data\['\zsuser\ze']/session\u&/g
& refers to the entire matched text (delimited by \zs and \ze if present), so it becomes 'user' in this case. The \u when used in a replacement string makes the next character upper-case. I hope this helps.
Search and replace in vim is almost identical to sed, so use the same escapes as you would with that:
:%s/\$data\['user'\]/$data['session']/g
Note that you only really need to escape special characters in the search part (the part between the first set of //s). The only character you need to escape in the replace part is the escape character \ itself (which you're not using here).
The [ char has a meaning in regex. It stands for character ranges. The $ char has a meaning too. It stands for end-line anchor. So you have to escape a lot of things. I suggest you to try a little plugin like this or this one and use a visual search.

Resources