How to search a particular word in a file in Linux using vi

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

You can go to a particular line or word in a file using vi in several ways:

  • To make vi start at a particular line in a file, add +line_num to the command you use to start vi. Replace line_num with the line number, for example: vi +36 foo.c
  • If you're already in vi, you can use the goto command. To do this, press Esc, type the line number, and then press Shift-g. If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file.
  • You can also use the ex command line to go to a line. (For information about the ex mode, see Use the vi text editor) For instance, if you wanted to go to line 14, you could press Esc and then enter: :14
  • To search forward for some text, use the / (forward slash) command. Press Esc and then enter /pattern, replacing pattern with the text for which you want to search forward. For example, to find every instance of the word "blimp", enter: /blimp To look for the next occurrence after the first, either press n or press / again and then press Enter. To go back to a previous occurrence, press Shift-n. To search backwards in a file, use the ? command.
  • To see what line you're on at any time, press Ctrl-Shift-g. To number all lines, press Esc and enter: :set number

Vim – short for Vi Improved – is a powerful open-source command-line text editor clone of the good old vi editor. It is highly configurable with an extensive manual and ships with a wealth of features, including syntax highlighting and color codes, comprehensive plugin support, and search and replace, to mention a few.

Searching text or a string by manually scrolling up and down can be a daunting and time-consuming undertaking. Thankfully, the vim editor has a faster and convenient way of doing this.

Main modes provided by Vim

Vim provides three main modes: command mode, insert mode, and visual mode.

By default, the Vim editor is first launched in command mode. In this mode, key-presses are inactive, and a user cannot insert text or modify the file. However, you can navigate up and down, left and right, using the following keys:

k – Moves up one row. (Equivalent of Arrow up key )

j – Moves down one row. (Equivalent of Arrow down key )

l – Navigates one character to the right or moves forwards. (Equivalent of Arrow right key)

h – Navigates one character to the left or moves backward. (Equivalent of Arrow down key )

You can also prefix the keys with a numeric letter to move up or down a certain number of rows or move forward and backward a certain number of characters. For example,

6k – moves up 6 rows

4j – Moves down 4 rows

The insert mode

This mode allows you to type in text and make changes to a text file as you deem fit. The insert mode can be accessed from the command mode by pressing the following keys.

The ‘i’ key ( insert ) allows you to insert a character at the cursor’s current position.

The ‘a’ key ( append ) – This moves the cursor one character to the right and gets you to insert mode.

The ‘o’ key – This creates a new line below the current row and switches to insert mode.

The visual mode

The visual mode is usually used to highlight text, similar to clicking and dragging with a mouse. To start making a text selection, simply type ‘v’ then leverage the arrow keys to highlight text.

Perform a basic search in Vim

To search text, you need to be in command mode. If you are in insert mode, simply press the ‘ESC’ key.

To perform a basic search of the string or text that you want, move to the beginning of the file and simply press the forward-slash ( / ) key. Then type the search string and press ENTER on the keyboard to start searching.

How to search a particular word in a file in Linux using vi

The forward-slash ( / ) key performs a forward search. It searches for the string or pattern from the current cursor position right up to the end of the file. To search for the next pattern, simply press the letter n on the keyboard.

To search backward, press the question mark symbol ( ? ), type the search string, and press ENTER on the keyboard. This searches the string from the current cursor position to the start of the file.

NOTE:

The search operation searches for a string or pattern and not the entire word. For instance, if you search for the string ‘form’, the search functionality will return results even when the string is present in more powerful or whole words such as ‘formal’ and ‘uniform.’

Search for a complete word

To search for a complete word, begin by pressing the / or? Symbol. After that, type the \< symbol to mark the beginning of the word to be searched then /> to signify the end of the search word. Then finally, hit ENTER to commence the search.

For instance, to perform a forward search of a pattern, run:

Here, we are searching for the complete word – ssh – in the /etc/ssh/sshd_config configuration file.

How to search a particular word in a file in Linux using vi

Ignore case sensitivity

By default, Vim is case sensitive, and so is the search pattern. To ignore case sensitivity, suffix the search pattern with the \c operand. For example, /path\c searches for any occurrence of the string ‘path’, whether upper or lowercase.

How to search a particular word in a file in Linux using vi

Another way you can achieve this is to press the ESC key followed by a whole colon followed by the text set ignorecase or the short form, set ic.

How to search a particular word in a file in Linux using vi

Next, hit the ENTER key. After that, type the ( / ) followed by the search pattern. In the example below, notice how we get the uppercase string of the pattern PATH.

How to search a particular word in a file in Linux using vi

Search the history of searched strings

Vim keeps a history of all the search items. To view the strings searched, simply type / or ? in command mode and press either the Arrow up or Arrow down keys to scroll through previously searched patterns.

Wrapping up

That summarizes how you can search for strings, patterns, or complete words on the vim editor.

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.

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.

special characters

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.