Lesson 5 of 26
Basic Searching inside a file in VIM
In this lesson
Roy searches inside a file by typing a forward slash and the text, then pressing n to go to the next match; the search is case sensitive by default and wraps around. He turns on incsearch to highlight the search while typing and hlsearch to highlight all matches, and removes the highlighting with set nohlsearch. A question mark can also be used to search.
Transcript
[00:07] (external link, opens in new tab) Let's talk about searching stuff in Vim. So, the way you search stuff in Vim is that you have two options in the command. Basically, you just type either the forward slash like this and let me just make sure Vim is in focus. So, I type forward slash and now I just type the thing that I'm looking for. So, let's say I'm looking for file type. I just have to type the first part of that. If I press enter, it's going to take me to the first found match. And I can then press N to go to the next match.
[00:37] (external link, opens in new tab) And obviously, there's only one. Let's say I look I'm looking for the word plug. I'm going to type slash plug. And by default, this is case sensitive, which is usually what you want, but we'll talk about case insensitive in a second. If I type plug now, I'm getting to the first found match. If I press N, I go to the next one and then it basically circles around. There is one helpful option you can set by saying command
[01:08] (external link, opens in new tab) Set incremental search. So incsearch and this basically highlights the search as I'm typing it. So if I type slash last status, I can see already where it's being found. If I type slashplug, I can see the first highlight. Another option I can set is to say set hlsearch, which means basically highlight all the matches that you find by default.
[01:40] (external link, opens in new tab) So if I press this, I can see all the matches that were found. Let me search something else. Let's say I'm going to look for set and I can see the first match. But if I type enter, I can see all the matches and I can press N to move between them. Now, how do I remove the highlighting? Because it's going to stay there. I have to type set nohlsearch, which basically removes it. Now, in a later video, I'm going to show how to create custom keyboard bindings so that you can remove the hlsearch just by with the keyboard shortcut.
[02:20] (external link, opens in new tab) And of course I said that slash is one of the ways to search, but you can also use the question mark. So if I type question mark set, I get the same result basically. And because I don't have hlsearch enabled, then I don't see all the types, all the matches that I