How to Search in Vim Text Editor

How to Search in Vim

This article explains how to search for words and expressions using Vim. Vim is ubiquitous, some tips and tricks can help you in everyday work life. So! Let’s explore simple searching, with Vim.

Find a string

Starting in normal mode, press forward-slash (/). Type in the string you’re looking for, and press enter. Press n to find the next occurrence. Once you reach the bottom, Vim loops back to the top.

Reverse Search

If you want to find all previous occurrences of a word, use “?” instead of “/”. Or while already searching with forward-slash, press shift+n to look at the previous match.

Match Word

There are two ways to match a whole word and not a string.

Way number one! Navigate to the word via the cursor, and press “*”. This will search for the next whole word and will change your Vim search parameter to:

/\<word>\

If you press “#” (your friendly neighborhood octothorp), you’ll find the previous match. The whole word of course.

This is the second way: While searching with “/” or “?”, use the following search format:

/\<word\

And if you forget what the format is, press while your cursor is on any word. Do note, “/” and “?” have a search direction. The first searches top to bottom, and the latter searches bottom to top.

If you press n while in “/” (forward-search) it proceeds down the file. If you press n while in “?” (reverse-search) it proceeds up the file.

If you change to whole word search with # or *, those also shift you into forward or reverse search modes. Shift+n, reverses the direction of your current movement, but not the mode. Just a caveat, I don’t run into it much, since I usually forward search.

Ignore Case

If you’re uncertain about what word you’re looking for, or if you’re looking to fix typos, ignoring the case is a great boon. To disable case sensitivity:

:set ignorecase
#OR
:set ic

There you go. Now your search patterns will ignore case sensitivity. If you want to revert, either close Vim or issue the commands:

:set noignorecase
#OR
:set noic

If you want it enabled for a single search:

/<word>/c

Will enable it for that search pattern. Note: if you press * or #, it will remove the c parameter.

Advanced Options

There are some tips and tricks that can help you navigate quickly with Vim. One that’s been helpful in my log parsing days:

vi +/<time> *.log.
vi +/Nov\ \17 syslog #Start Vim at November 17th in sus-log for suspicious activities
vi +/Nov\ \17\ \14 #Start at November 17th, 14PM to find the suspect

This starts the instance of Vim at the specified time, searching through Syslog. Helping you narrow down suspicious activity. Vim also has a search history (which I stumble upon accidentally, often). You can enter it by typing “/” or “?” and then using the arrow keys to navigate. “:h” will take you to the help menu.

Conclusion

That’s it. All the search options you should need. If anyone has additional tips or tricks feel free to add them in the comments section. Look through more articles for improving your Vim skillset, find out how to find and replace in vim, or check out the official Vim wiki.

SHARE:
nv-author-image

Samuel Berry

I'm a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I'm writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That's what builds the "user-friendly" experience. Not only design, but inclusion. Talk to me about Linux, I'm here to learn by teaching.

Leave a Reply

Your email address will not be published. Required fields are marked *