Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the next occurrence of a variable in vim

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!

like image 402
canavanin Avatar asked Dec 10 '10 12:12

canavanin


People also ask

How do I get to next occurrence in vi?

vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return. Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.

How do we search forwards for a pattern in vim?

One can search forward in vim/vi by pressing / and then typing your search pattern/word. To search backward in vi/vim by pressing ? and then typing your search pattern/word. Once word found in vim, you can press the n key to go directly to the next occurrence of the word in backwards.

What does control f do in vim?

CTRL-F is a vital keystroke to control vim - it provides the page-down behavior and without that I have to find the actual page-down key which takes my fingers off the home row.

What is the command to go to the next match from your search Linux?

Use N to move to the previous occurrence. For the info command, type s to search, then enter your search term or regex. It will bring you to the first occurrence. To move to the next match, type } , to move to the previous occurrence type { .


1 Answers

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 /\< 
like image 180
DrAl Avatar answered Oct 09 '22 20:10

DrAl