Sublime Text 3: I can't search a string with a dollar followed by underscore ($_GET, $_POST, etc.) - search

I can search the following without problems:
_GET
$variable
However, sublime fails to search $_ (p.e. $_GET.) I have tried to escape it somehow:
$\_GET
\$_GET
$__GET
I'm on Ubuntu 14.04LTS

Turn off the regular expressions search. It is the button on the far left of the search field (in this picture currently selected):
With regular expressions turned off:
Although I'm not sure if this would fit your exact problem since you tried escaping using \$_, this answer may still help for posterity.
Did you also make sure "whole word" search is turned off? That's the 3rd button from the left (next to the Aa)
With whole word turned on:
Failing with the attempted escaped \$_:
And it succeeding with _GET:
Note that whole word search of $_ would succeed if there was a whole $_ phrase, surrounded by whitespace. For example with whole word search on:
I am a sentence with the keyword $_ which will be matched.
would work, whereas:
I am a sentence with the keyword $_GET, which will never match. $_POST, $_REQUEST, and $_SERVER won't work either.
would break the whole word search.

Related

Replace one word in searched result vim

Is it possible to replace a section in the searched result? for example I have currently
<stringProp name="Argument.name">revision_no</stringProp>
<stringProp name="Argument.value">1</stringProp>
I want to replace the number (1) with ${var} on the 2nd line, and all other information remains the same.
If I type
:%s/revision_no<.*\n.*value">[0-9]\*/revision_no<(all the characters...) value">${var}/g
I might lose all the format(indentation involved)..
So I am wondering if there is a way to just replace "1" with ${var} in the whole search result.
`
You can capture the other matching text (that you want to keep), and then reference that exact text in the replacement. The \(...\) is a capture group (:help /\(), and \1 references it (the first such group) in the replacement. This is the traditional way, and it also works in sed and many other regular expression-based tools:
:%s/\(revision_no<.*\n.*value">\)[0-9]\+/\1${var}/g
Alternatively, in Vim, you can assert that certain surrounding stuff matches without actually including it in the match. This "cutting" is done via \zs (start match here) and \ze (end match here):
:%s/revision_no<.*\n.*value">\zs[0-9]\+/${var}/g

replacing part of regex matches

I have several functions that start with get_ in my code:
get_num(...) , get_str(...)
I want to change them to get_*_struct(...).
Can I somehow match the get_* regex and then replace according to the pattern so that:
get_num(...) becomes get_num_struct(...),
get_str(...) becomes get_str_struct(...)
Can you also explain some logic behind it, because the theoretical regex aren't like the ones used in UNIX (or vi, are they different?) and I'm always struggling to figure them out.
This has to be done in the vi editor as this is main work tool.
Thanks!
To transform get_num(...) to get_num_struct(...), you need to capture the correct text in the input. And, you can't put the parentheses in the regular expression because you may need to match pointers to functions too, as in &get_distance, and uses in comments. However, and this depends partially on the fact that you are using vim and partially on how you need to keep the entire input together, I have checked that this works:
%s/get_\w\+/&_struct/g
On every line, find every expression starting with get_ and continuing with at least one letter, number, or underscore, and replace it with the entire matched string followed by _struct.
Darn it; I shouldn't answer these things on spec. Note that other regex engines might use \& instead of &. This depends on having magic set, which is default in vim.
For an alternate way to do it:
%s/get_\(\w*\)(/get_\1_struct(/g
What this does:
\w matches to any "word character"; \w* matches 0 or more word characters.
\(...\) tells vim to remember whatever matches .... So, \(w*\) means "match any number of word characters, and remember what you matched. You can then access it in the replacement with \1 (or \2 for the second, etc.)
So, the overall pattern get_\(\w*\)( looks for get_, followed by any number of word chars, followed by (.
The replacement then just does exactly what you want.
(Sorry if that was too verbose - not sure how comfortable you are with vim regex.)

Issue with escape characters in Watir/Cucumber

Was hoping someone could help me with an issue I am having with escape characters in Cucumber/Watir.
I have automated tests setup. When I perform a search, 1 of the assertions I use to verify that the search has returned the correct result is to check the page for text. So my code looks like this:
Then /^I should see the following text: "([^"]*)"$/ do |str|
assert #browser.text.include?(str)
end
Here I pass in the text to search for in the string variable. e.g nike, reebok etc
So in my feature file the step is like this:
Then I should see the following text "search results for nike"
This works fine apart from 1 issue. 1 of the sites I am testing has decided to put the search term in double quotes i.e - search results for "nike"
As a result this screws up my test as I need to include the quotes as part of the search term. Therefore I need to put the word nike in escape quotes or else cucumber will recognise the first quotation around the word nike as a closing quotation. (as there is already a double quotes before it)
I have tried various different escape characters but nothing seems to work. For example I have tried the following:
\" – double quote
\\ – single backslash
Has anyone experienced similar problems and if so, how did you overcome the problem?
Thanks!
You need to change the regex rather than the string.
Problem: Your current regex says "([^"]*)", which says to match all characters between the quotations that are not quotations. This is not good given that you want to include quotations.
Solution: Change the step to the following:
Then /^I should see the following text: "(.*?)"$/ do |str|
assert #browser.text.include?(str)
end
The .* says to match all characters between the quotations. The ? makes the search lazy (instead of greedy). The ? is optional in this case, but would be important if there were additional parameters being captured. A good explanation of the greedy vs lazy can be seen at http://www.regular-expressions.info/repeat.html.

searching whole word in Vim (dash character)

I know for searching a whole word I should use /\<mypattern\>. But this is not true for dash (+U002d) character and /\<-\> always fails. I also try /\<\%d45\> and it fails too. anyone know the reason?
Edit2: As #bobbogo mentioned dash is not in 'iskeyword' so I add :set isk+=- and /\<-\> works!
Edit1: I think in Vim /\<word\> only is valid for alphanumeric characters and we shouldn't use it for punctuation characters (see Edit2). I should change my question and ask how we can search punctuation character as a whole world for example I want my search found the question mark in "a ? b" and patterns like "??" and "abc?" shouldn't be valid.
\< matches the zero-width boundary between a non-word character and a word character. What is a word character? It's specified by the isk option (:help isk).
Since - is not in your isk option, then - can never start a word, thus \<- will never match.
I don't know what you want, but /\>-\< will match the dash in hello-word.
Could always search for the regex \byourwordhere\b
As OP said. In order to include dash - into search just execute:
:set isk+=-
Thats all.
Example: When you press * over letter c of color-primary it will search for entire variable name not just for color.

Possible to highlight matching quotes in vim?

With the syntax highlighting in vim, I get the handy feature where the matching paren or bracket will be highlighted when I put the cursor over it. Is it possible to do the same thing for quotes?
While not eloquent, one workaround is to select everything inside of matching quotes. You can do this by using the command:
vi"
This will select everything in-between the quotes. However, you won't get proper results with nested quotes as it will match the first found ".
The problem with quotes is that they are symmetrical. It would be very hard to determine which quotes belong with each other.
For instance: "Which \"quotes\" go with each other in this statement?"
This has been discussed on the vim mailing lists a few times, as well as in the bug trackers of a few of the auto-delimiter type plugins. In every case that I've seen, it's been decided that this is better left as is.
The solution is here: Stackoverflow in matchquote except it has the unfortunate limitation that only the current line is considered.
matchit seems to comes close by allowing defining of multi-line matches of words such as if/endif but still no multi-line possibility that I can figure out to get matching for " and '.
VIM already highlights quoted text in a different color, so you can easily identify strings. Do you really need it to match quotes when the whole string is already highlighted?
From :h matchparen
The characters to be matched come from the 'matchpairs' option. You
can change the value to highlight different matches. Note that not
everything is possible. For example, you can't highlight single or
double quotes, because the start and end are equal.

Resources