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

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

Related

Removing everything after last hyphen in a string in Bash script? [duplicate]

This question already has answers here:
How can I remove all text after a character in bash?
(7 answers)
Closed last month.
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg should result in This-is-a-filename
You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*operator removes all beginning with the last hyphen.
You're looking for a sed within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers

Vim: Add closing buckles, brackets, quotation marks etc. automatically? [duplicate]

This question already has answers here:
Vim plugin for 'auto-closed' parenthesis?
(5 answers)
Closed 6 years ago.
This is something what I miss from other editors. I'm looking for a plugin/config which adds closing mark for some characters automatically.
For example, when I type (, it add ) and prompt will be between it. Similarly with {, " etc. This would be very helpful for me. I know I can do it using Vim command, but my goal is do it automatically.
There is a plugin named auto-pairs.vim available in github. See here : https://github.com/jiangmiao/auto-pairs
It can automatically insert closing brackets and quotes and puts prompt in between both.
It is smart and doesn't insert matching brackets for escaped brackets. It works even if you nest different brackets.

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

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+/+//+

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 :) )

What's the meaning of this sed command? sed 's%^.*/%%' [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I saw a bash command sed 's%^.*/%%'
Usually the common syntax for sed is sed 's/pattern/str/g', but in this one it used s%^.* for the s part in the 's/pattern/str/g'.
My questions:
What does s%^.* mean?
What's meaning of %% in the second part of sed 's%^.*/%%'?
The % is an alternative delimiter so that you don't need to escape the forward slash contained in the matching portion.
So if you were to write the same expression with / as a delimiter, it would look like:
sed 's/^.*\///'
which is also kind of difficult to read.
Either way, the expression will look for a forward slash in a line; if there is a forward slash, remove everything up to and including that slash.
the usually used delimiter is / and the usage is sed 's/pattern/str/'.
At times you find that the delimiter is present in the pattern. In such a case you have a choice to either escape the delimiter found in the pattern or to use a different delimiter. In your case a different delimiter % has been used.
The later way is recommended as it keeps your command short and clean.

Resources