VIM search for characters starting with ampersand - vim

I have to search for something like &variable (and replace with &variable.test). I could just do \&variable, but vim also shows me all characters starting with &variable (&variableXXX for instance), which I do not want. I thought this - /\<\&variable\> would work, please let me know where am I going wrong.

The first problem is that \& is not a literal & its a special regex match that matches branches in vim. To match a literal & just use &.
The second problem is that \< matches the beginning of a keyword by looking to see if the next character is a keyword. However & is not normally a keyword so it always fails.
To get around this you can make & a keyword by doing
set iskeyword+=&
Then you can use the regex \<&variable\> to match the things you want.
Relevant help pages :h iskeyword, :h /\< and :h \&

Short answer: use /&variable\>.
You don't want the \ in front of the &. See help \&.
Your \< is in the wrong place -- it belongs after the &, not before, since & is not a keyword character (:help keyword).
In fact, you don't need the \< at all. You could search for &variable\> and you would find &variable but not &variableXXX. You don't have to use \< just because you're using \>.

Related

What does the + sign mean in vim patterns when used to quote a pattern?

The vim help example gives this line for a syntax statement:
:syntax region String start=+"+ skip=+\\"+ end=+"+
Is the + sign here an alternative to quoting? I couldn't find a reference to this in the help pages on patterns.
An introduction to patterns, see :h pattern.txt. As in the example of chapter 10 (:h :match), :match MyGroup /TODO/, instead of // any character can be used to mark the start and end of the pattern.
Like your question (:h syntax.txt), this is explained in Chapter 8 (:h :syn-pattern) :
In the syntax commands, a pattern must be surrounded by two identical characters. This is like it works for the ":s" command. The most common to use is the double quote. But if the pattern contains a double quote, you can use another character that is not used in the pattern. Examples: :syntax region String start=+"+ end=+"+ skip=+\\"+
Just like the :s command, see :h :s and :h pattern-delimiter, the benefit and convenience is the handling of escape characters. Using the :s command as an example is more convenient for you to verify quickly.
If you want to replace a with b, you can use :s/a/b/ or :s+a+b+. But if you want to replace / with //, using delimiter / require :s/\//\/\//, we can change the delimiter / to + (:s+\/+\/\/+). Actually, here we no longer need to escape /, so in the end there is only a concise :s+/+//+.
Back to your question, if the pattern contains a lot of double quotes, we can use another character that is not used in the pattern as delimiter (e.g. +), otherwise each double quote in the pattern needs to be escaped.
That sample command in your question isn't provided without context. It is provided as an illustration for the paragraph right above it:
In the syntax commands, a pattern must be surrounded by two identical
characters. This is like it works for the ":s" command. The most common to
use is the double quote. But if the pattern contains a double quote, you can
use another character that is not used in the pattern.

Can not find the good syntax for a vim sustitute pattern

I have a bunch of t_ prefixed fonction names in a header file and i would like to remove this prefix from some of them.
I have ended by typing :
:s/\(\s+\)t_\([^(]+\)(/\1\2(/c
But vim complains with Pattern not found
What is wrong in my pattern ?
You forgot to put a backslash before + to give it its quantifier meaning. You can also probably simplify it using \< to match the start of a word instead of capturing spaces.
I suggest breaking the problem down, first the search:
/\v\s+\zst_\ze.*\(
Now the substitution:
:s///\c
We can reuse a search pattern simply typing an empty search on the substitution
OBS: I suppose your functions have () at the end of the line, so .*().
Another thing; \zs and \ze to match only t_, for more see: :h \zs.
If you are using neovim you can see what happens befor hitting enter, just put these lines on your init.vim:
if has("nvim")
set inccommand=nosplit
endif

vim multiple character substitute regex issue

I am little new to Vim world. I am trying to substitute *=, ~=(actually [special char]=) in to [whatever is symbol]=(adding space both sides). Here is my substitute command:
:%s/[~,\*]=/ = /g
the problem in this case is that I am not able to add respective special symbol before the equal sign. Can you help me...
This is a classic capture and replace use case. Capture the symbol part by enclosing it in \(...\), and then reference it in the replacement part via \1. You'll find more details at :help s/\1 (or :help :substitute in general):
:%s/\([~,\*]\)=/ \1= /g
Alternatively, you can start the match only on the = with \zs. This asserts that the symbol part is there, but as it isn't included in the match, you don't need to reference it:
:%s/[~,\*]\zs=/ = /g
The same trick can be applied with \ze at the end. As you can see, this often results in shorter commands.
This is probably the simplest answer to your question:
:%s/[~,\*]=/ & /
An& in the replace segment means 'entire match'.

vim: search and replace for "&"

In order to replace each occurrence of "&" to "&&" in the range from the current line to 30 more lines on, I issue :.,+30s/\\&/\\&\\&/g but Vim says "Pattern not found". I'm escaping the special character "&" by double backslash "\".
Try not escaping everything - :.,+30s/&/&&/g seems to work for me.
You don't need to escape the ampersand in the first part of a regular expression. It only has special meaning in the second (replace) part.
You are looking for patterns that say "\&" and replace them with patterns that say "\&\&".
The simple way to replace is just:
:.,+30s/&/&&/g
It can also be useful remark that if you need to change the & position for example: _& to &_ the & has to be escaped only in the replace statements of the substitute expression:
:.,+30s/ &/\& /g
^

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