The vi or vim is a console based text editor in Linux. It is probably the most useful of Linux applications, in that you can quickly view and edit the text files right at the command shell. When viewing the text files, one of the useful features is the ability to search for a word or some text in the file.
The vi editor provides some powerful search and replace features. We will look some of the commonly used search feature that come in handy.
search single word or string
You can search for a single word or a character string by using the forward slash (/). A character string is one or more character that occur in succession and can be a single word or part of a word. It can contain letter, number, spaces, punctuations etc.
When the file is open in the vi editor, change the editor to the normal or command mode by pressing Esc (or Escape) key. The command mode is the default mode. When in command mode type the forward slash (/) and followed by the string (or word) you want to search. You can see the string you typed at the bottom left side of the vi editor or the screen.
Once you have typed in the string you want to search, hit enter and the vi editor will place the cursor at the first occurrence of that string in the editor and highlight the string. So, if you wanted to search for the string apple, then type /apple and hit enter.
After finding the first occurrence of the string, you can type n to find the next occurrence of the same string in the editor. The editor will place the cursor at the next occurrence from the current cursor location. Similarly, you can find the previous occurrence by typing N (n in caps).
search multiple strings or words
Sometimes you would want to search for multiple strings such that it matches either of them. Let’s say you want to search for the occurrence of either apples or oranges. You can separate the strings in the query string by using the pipe (|) character.
When the file is open in command mode in the vi editor, type forward slash (/) and the type the strings separated by the | character. So in this case, you will type /apples\|oranges into the editor. You will need to escape the | character so that the editor identifies it as a separator and not use it as a literal character.
search just the word or the whole word
As you might have already noticed, the default search will match any part of the string which makes it difficult to search for whole words in the file. In order to match the beginning of a word you need to use the less than (<) character and to match the end of the word you will use the greater than (>) character. Of course, you will need to escape both < and > characters with the backslash.
So, as always type forward slash (/) and then the backslash and < character, followed by the word you want to search. You can finish the query with another backslash and then the > character. So, to search for the word apple, you will type /\<apple\>. This will find all occurrences of apple but will not match apples.
advanced search and special characters
There are several special characters that you can use to further filter or narrow your search patterns. We will see some of the commonly used expressions.
- The ^ (caret) character will match the start of a line in the file. So, the search term /^apple will match any string that starts a line with apple.
- The $ (dollar) will match the end of the line. The search term /apple$ will only match those strings that has apple at the end a line.
- The . (dot) will match any one or single character. So, the search term /apple. will match strings like apples or appled etc etc.
- The * (asterisk) will match zero or more occurrence of the preceding character. So, the term /apples* with match strings like apples, appless and applesss.
- The square brackets ([..]) can be used to match multiple characters. So, a search term /[ky] will match the character k or the character y. You can also specify character ranges using square bracket notation. So, the term /[k-y] will match all characters between k and y.
You can use the above characters together as well. An example of using * and . with the bracket notation can be something like: /a[p]*le. that will match strings such as apples, apppples, ales etc. Another example would be /a[p-s]*le that will match strings like apple, aprole, appoorrle etc etc.
We covered the way to search in a forward fashion in the previous sections. The use of forward slash (/) at the start of the search term denotes that the search is to be done in a forward fashion with in the file. You can always use n and N characters to find matched strings in a forward or backward fashion respectively.
Alternatively, if you wish perform a backward search instead of forward search then you should use the ? (question mark) instead of the / (forward slash) when performing the search. So, the term ?apple will search backwards for the string apple in the file. The n and N now will now traverse in opposite directions compared to the forward search.
case insensitive search
You must have already noticed that search in the vi editor is case sensitive by default. So, if you want to do case insensitive searches you will need to turn it off. You can do that by using the command :set ic when in command mode. You can turn it back on using the command :set noic again when in the command mode.
You know by now that there are several characters that have special significance in the search term. Some of these characters are / (forward slash), ? (question), ^ (caret), $(dollar), . (dot), * (astericks), | (pipe) etc. You will need to treat these characters differently if you want use them in the search string. You can do so by escaping them by using the backward slash (\).
So, to search for the term question? , you will use the term /question\? which will find all the strings that match question?. The backward slash before a character will treat that character as a literal character rather than a meta character.