I have a file, that contains spaces at the end of the line and they should be removed.
When I use the following command:
%s/\s+$//
Vim shows me an error that pattern is not found. What is wrong here?
NOTE: actually, I can use the %s/\s*$// command but I want to understand the root cause of the issue.
Vim shows me an error that pattern is not found
You need to escape the quantifier +.
:%s/\s\+$//g
You might also want to refer to Quantifiers, Greedy and Non-Greedy.
You can use it like this:
%s/ \+$//
OR:
%s/\s\+$//
As + needs to be escaped in vim regex.
Using the "very magic" mode with \v, you only need to escape alphanumeric specials:
%s:\v\s+%(//.*)?$::
(Here : is used as a separator, instead of /. You can use almost any non-alphanumeric ASCII character.)
another way to do it:
:%s/ *$//g
if you do not want to input '\' chars ;)
Related
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
/\/\*
This is my first time trying to use a regex for deletion.
The regex:
/net=.+\.net/
as shown here matches a string that starts with net= some random characters and ends with .net
However, when using it in vim:
:g/net=.+\.net/d
I simply get Pattern not found: net=.+\.net
I am guessing that vim uses a slightly different format, or do I need to escape the characters =, . and + ?
:help pattern is your friend. In your case, you need to escape + or prefix your whole pattern with \v to turn it “verymagic”.
Do not escape =, it would turn it into the same thing as {0,1} in some regexp engine, namely a greedy optional atom matcher.
I want to remove strings in the form of the following where some-text is a random text string.
$('#some-text').val();
I've tried various things but I think the $ sign is messing things up since it's used in regex.
You need to escape some characters.
Try this -
\$\('#[^']*'\)\.val\(\);
Try this regex by escaping special chars:
\$\(.*\).val\(\);
To avoid escaping the special characters, you can use \Q - \E pair to surround the part where you want the regex engine to interpret literally:
\Q$('\E<your-regex>\Q').val();\E
Replace <your-regex> with your regex to match the selector, or whatever it is.
How to write regex to match if only first character is . ?
I'v been trying this:
hide_file={.*}
But unfortunately, it will find all files that has . in it.
For example:
/home/user
.bashrc
.bash_history
some_text.csv
foo.json
In this example I would like this regex to affect only first two files.
P.S
That's the requirement:
Supported regex syntax is any number of *, ? and unnested {,} operators. Regex matching is only supported on the last component of a path, e.g. a/b/? is supported but a/?/c is not. Example: deny_file={*.mp3,*.mov,.private}
Simply use
^\s*?\..*$
See http://regex101.com/r/oW1xP3 for a live demo
If you are sure there are no whitespaces in front of your input remove the \s*?
The trick is to anchor ^ the regex to the beginning of the string.
^\. will match any string that begins with a period. *Note: * you will need to escape this regex appropriately for your programming language.
hide_file={^\.}
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
/\/\*