Find the next occurrence of a variable in vim - search

I would like to know if/how I can make vim look for the next occurrence of a variable. Let's say the variable's name is simply 'n', then /n would give me all occurrences of that letter, which isn't always terribly helpful. I guess I could create a regex to solve the problem, but I wondered whether there was some command/keystroke I simply don't yet know about; and as all my googling has been to no avail I decided to put a question on here.
Thanks a lot for your help!

If you have the cursor over the variable in question, you can press * and it will search for the next occurrence or # will search for the previous one.
This is equivalent to typing:
/\<n\>
(\< matches on the start of a word and \> matches on the end of word). The only difference (for reasons I'm not sure of) is that * and # don't pay attention to the 'smartcase' option.
See:
:help *
:help /\<

If you press n in command mode it will give you the next match of your search.
More detail:
/ will start forward search
? will start backward search
n will give you the next result in the direction you are searching
N will give you the previous result wrt the direction you are searching in

Related

Replace n-th occurrence of a word

I am using vim to edit a huge file-I want to replace the 100th occurence of 'Luke' with 'The chosen one'. I searched the Internet for a possible solution and I guess I should do somethink like /Luke and then hitting the 'n' key 99 times, but I find this a bit tedious. Is there any easier way to get this done?
next supports a count
99n
should do
Otherwise, you could play with :global (it's a little bit overkill)
:let c = 0
:g/Luke/let c+=1|if c==100|s//This one!/|endif
You can script something, using feedkeys() function and using :s commadn with the "c" flag. On the mailinglist, this was once suggested.

Vim - Find pattern on currently line ONLY

I'm wondering if there is a way to find a pattern, but restrict it to the current line. Basically, the equivalent of /PATTERN but restricted to the current line, rather than the entire document.
I've tried :s/PATTERN, but that deletes the pattern from the line and places my cursor at the beginning of the line, which is not at all what I need. I was hoping you could search without replacing...
I'm hoping to use this for a macro in order to place my cursor at the start of that pattern, as would happen when you do /PATTERN on the entire file, so anything that is macro-friendly is even better.
Any vim users out there that might have an idea?
EDIT: 0/PATTERN in a macro would work for my current need, but I'm hoping there's a more specific way to restrict the search.
ANSWER: There's a few ways posted in here so far, but the one I like best right now is using Shift+V to select the current line visually, followed by /\%V to search only in the visual selection. Then Shift+V again will turn off the visual mode.
My knowledge about macro is limited, but interactively, you can select current line with Shift + V, and then do /\%Vsearch (see http://vimdoc.sourceforge.net/htmldoc/pattern.html#/\%V).
try to Find first character of the Pattern by typing
f <letter>
It's not exactly what you need but can help to solve the problem.
/\%9lsearch
Where \%9 means line number 9.
Typing in the line number is still a bit lame. You can ctrl+r= followed by a vim expression and enter to evaluate the vim expression and insert its output. line('.') will return the line of the cursor.
In one complete step
/\%<c-r>=line('.')<cr>lsearch
For more help see:
:h /\%l
:h i_CTRL-R
Place the cursor on the line you want to search in
Select it with shift+v
Type / to begin searching
Prefix your term with \%V, e.g. \%Vabc to search for abc in only the visually selected blocks (in our case the single line)
You can search without replacing by using
:s/PATTERN//gc
Then press n to skip the replacement. If the pattern is not found, you won't even be asked.
You could also just highlight the current line or the range of lines.

Vim find last occurrence of

In a large buffer, I'd like to scroll down to the last occurrence of pattern pattern.
If I am at the first occurrence, it is easy enough to search for the pattern /, reverse the move to next occurrence n with N and get to the last..
If I am in the middle of a sequence of occurrences.. is there a better way to jump?
An easy way to find the last occurrence is to jump to the end and search backwards:
G?foo<CR>
In fact, there's an even easier way if you were already searching for something. Just jump to the end and search backwards for the thing you were just searching for:
GN
Simple as that.
Edit: if your search occurred on the very last line, then GN would skip over it to the second last occurrence. Using ggN would solve this problem. (And gg?foo<CR> for similar reasons.)
A potentially longer solution:
:vim foo % | clast
You can try searching backward via ?
:1?SEARCH_PHRASE
Another solution is:
G/\(.\{-}\zs\(foo\)\)\{1,}<CR>

Go to the next same word in Vim without using search

Is there a shortcut in Vim for going to the next word which is the same as the word cursor is on? It should work without typing the word with search command /.
* goes to the next matching word and # goes to the previous matching word. * is so useful it's sometimes called the super star.
* and # are your friends ( forward and backward directions respectively )
While all the answers here are correct, I thought it may be useful to provide a little more info.
What * actually does is perform a forward search for \<word-under-the-cursor\>. Because this is just a search operation, you can then navigate forwards and backwards to the next occurrences using n and N. This also means your previous search is lost. # is exactly the same as * except it performs a reverse search.
The \< and \> in the search string are word boundaries in vim's regex language which is what makes this work so nicely. It's also important to note that what is considered a "word" is determined by the iskeyword option. See :help word for more information.
To search the current word under the cursor use '*'.
to search backwards for current cursor word use '#'
gd and its variants are also very handy. It works slightly differently from *, in that it searches for the local declaration of the word under the cursor (or global declaration if none local exists). You can then search through the results with n like you can with superstar, and it will skip comments. It can be helpful when you're editing code.
:help gd

Search for string and get count in vi editor

I want to search for a string and find the number of occurrences in a file using the vi editor.
THE way is
:%s/pattern//gn
You need the n flag. To count words use:
:%s/\i\+/&/gn
and a particular word:
:%s/the/&/gn
See count-items documentation section.
If you simply type in:
%s/pattern/pattern/g
then the status line will give you the number of matches in vi as well.
:%s/string/string/g
will give the answer.
(similar as Gustavo said, but additionally: )
For any previously search, you can do simply:
:%s///gn
A pattern is not needed, because it is already in the search-register (#/).
"%" - do s/ in the whole file
"g" - search global (with multiple hits in one line)
"n" - prevents any replacement of s/ -- nothing is deleted! nothing must be undone!
(see: :help s_flag for more informations)
(This way, it works perfectly with "Search for visually selected text", as described in vim-wikia tip171)
:g/xxxx/d
This will delete all the lines with pattern, and report how many deleted. Undo to get them back after.
Short answer:
:%s/string-to-be-searched//gn
For learning:
There are 3 modes in VI editor as below
: you are entering from Command to Command-line mode. Now, whatever you write after : is on CLI(Command Line Interface)
%s specifies all lines. Specifying the range as % means do substitution in the entire file. Syntax for all occurrences substitution is :%s/old-text/new-text/g
g specifies all occurrences in the line. With the g flag , you can make the whole line to be substituted. If this g flag is not used then only first occurrence in the line only will be substituted.
n specifies to output number of occurrences
//double slash represents omission of replacement text. Because we just want to find.
Once got the number of occurrences, you can Press N Key to see occurrences one-by-one.
For finding and counting in particular range of line number 1 to 10:
:1,10s/hello//gn
Please note, % for whole file is repleaced by , separated line numbers.
For finding and replacing in particular range of line number 1 to 10:
:1,10s/helo/hello/gn
use
:%s/pattern/\0/g
when pattern string is too long and you don't like to type it all again.
I suggest doing:
Search either with * to do a "bounded search" for what's under the cursor, or do a standard /pattern search.
Use :%s///gn to get the number of occurrences. Or you can use :%s///n to get the number of lines with occurrences.
** I really with I could find a plug-in that would giving messaging of "match N of N1 on N2 lines" with every search, but alas.
Note:
Don't be confused by the tricky wording of the output. The former command might give you something like 4 matches on 3 lines where the latter might give you 3 matches on 3 lines. While technically accurate, the latter is misleading and should say '3 lines match'. So, as you can see, there really is never any need to use the latter ('n' only) form. You get the same info, more clearly, and more by using the 'gn' form.

Resources