How to Searching Word/Text in Vim?

The macOS, Unix, Linux, and *BSD family of operating systems have support for the text editors vi and vim. A free and open-source text editor is known as Vim. Several keys in the vi text editor can be utilized to perform searching. On a Linux or Unix-like system, the vi or vim text editors have been utilized for finding and typing words.

Find a Word in Vim

Vim editor is a powerful text editor which can use search a word phrase by typing the instruction or command /.

The instructions given below will lead you to find a word in Vim.

  1. First, open the Vim editor and find the file where the word you’ve been looking for is located.
  2. To return to your normal mode, enter the Esc key.
  3. Type / and whatever word you’ve been searching for. In this case, you would type /sir when looking for the word “sir.”
  4. To search for the word, push Enter. Vim is going to bring attention to the word’s initial outward appearance.
  5. Press n to look for the word’s successive repetition. Press N to navigate back to the previous instance.
  6. Push Esc to quit the search mode.

Here are a couple more ideas for using Vim to find words:

  • Using regular expressions can be used to detect deeper patterns. In this example, you would type /g.* to locate all words that begin with the letter “g.”
  • The operators or commands * and # can be applied to search for the present words after and before occurrences.
  • The :g/pattern/ a keyword can be utilized to look for every instance of a pattern throughout a file in its entirety.
Searching Word/Text in Vim
Searching Word/Text in Vim

Searching for words in Vim

Vim editor has a different way of looking for words by using commands or instructions such as forward search “/” using the backward search “?” and using search and replace s/old/new/

Here are the few ways given below to search for words in Vim.

  • Using forward Search: Press / and the keyword you want to look for to start a forward search. For instance, you would type /kishore to search for the word “Kishore“. The first mention of the word in the file is the one highlighted by Vim. The n key and the N key can be used to search for the word’s next and previous occurrence accordingly.
  • Using Backward search: Press? And the word you would like to hunt up to launch a backward word search. For instance, you would press ?kishore to bring up the word “kishore”. Every instance of the word in the file, from the very end to the start, is subsequently highlighted by Vim. The n and N keys can be used to look for the word’s next occurrence and previous occurrence.
  • Using Search and Replace: Press: and enter the command s/old/new/ to replace a word with another one while trying to locate it. For instance, you would enter s/kishore/ramesh/ in order to search for an expression such as “kishore” and replace it with the word “ramesh”. The word “ramesh” will then be substituted in place of all occurrences of “kishore” in Vim.

Search for a Word in Backwards

The ? sign can be employed instead of the /sign in Vim when doing a reverse word search. For instance, you would type ?ravi to search for the word “ravi” backward. The n and N instructions may be utilized as well to jump to the word’s subsequent or previous occurrence, depending.

Here are the procedures for using Vim’s backward word search feature:

  1. Begin the process by opening the file for which you want to search.
  2. To switch to command mode, press the ESC key.
  3. Type the word you want to begin looking for, followed by the? Sign.
  4. Then Push Enter after typing the?.
  5. Vim will reverse-search the word and display the first appearance.
  6. The n and N instructions can be utilized to move on to the following command. The instructions n and N can be used to go to the words next or before the occurrence.

Search for the Current Word

You can look for the present or current word in Vim editor by following different commands or instructions such as * and # keys, / and ? keys and g* and g#.

There are different solutions to search for the existing word in Vim editor.

  • Follow to use the * and # keys: Place the cursor on the phrase you would like to begin looking for in the normal mode of the Vim editor. Following that, push * in order to search for the word’s next mention or # to find its previous mention.
  • Follow to use the / and ? keys: In the normal mode of the Vim editor, When searching normally, enter the keyword you would like to show up after entering /. Vim is going to search for the word’s next appearance. Enter the word, followed by the? Character, in order to search for the previous occurrence.
  • Follow to use the g* and g# commands: In the standard mode of Vim editor, enter after typing g* and the word you would like to search for. Vim is looking for the word everywhere it finds itself in this particular buffer. Type gg next to g* followed by the keyword and push Enter to start looking for every example of the word throughout the entirety of the document.
  • Follow to use the: find command: In the command mode of the Vim editor, enter the term you wish to search for after typing. Vim will be looking for the phrase’s future repetition. Enter the term and the command:find! to begin looking for the previous occurrence.
  • Whenever you have finished the word phrase you are searching for, you can make use of the n and N keys to go to the next or previous appearance of the word phrase, respectively. You may also make use of the c and C keys to modify or remove the word accordingly.

Open File at Specific Word

Vim editor does not only search the word you specify. It also searches the file belonging to the word you mentioned to search.

You can apply the given below command to open the file at the specific word in Vim.

Code example

vim +/word file.txt

For case in point, to open the file kishore.txt and go to the word hello, you should use the given below command:

Code snippet

vim +/hello kishore.txt

The + symbol signifies Vim to access the file and soon after begin finding the word. The / Character signifies Vim to find forward for the mentioned word, and to prevent Vim from understanding it as a command, the mentioned term is enclosed in quotes. The command’s final argument is the file name.

The cursor will be positioned over the initial appearance of the word once Vim has accessed the file and discovered it. The file is able to be edited with normal Vim commands.

Find for Whole Word

Using the Vim editor, you can find whole words by flowing the / and ?, * and #, and \< and \>There are various solutions to search whole words or terms in Vim editor.

  • Find the whole word by following the / and ? commands: Type / shortly after the word you’re attempting to find in order to start a forward search for the whole statement. Type? After the keyword, execute the search in the past for that term. As an example, you would type /Suresh when looking up the term “Suresh” in future searches. Would you type? Suresh to look up the word “Suresh” backward.
  • Find the whole word by following * and # commands: Type * to search for the phrase under your cursor’s next appearance. Type # to get back to the search. If your cursor is presently over the word “suresh,” for example, entering * will result in searching for the word “suresh” moving forward. By typing #, you are able to identify the following instance of the word “suresh” backward.
  • Find the whole word by following \< and \> characters: In search of a whole word, you can apply the \< and \> characters to fasten the search to the starting and ending of a word phrase in that order. For a case in point, to look for the term “Kishore“, you could use the pattern \<kishore\>. This pattern would match the word “Kishore“, but not the “hellos” term.
  • Whenever you have searched a whole word, you can use the n and N instructions to move to the subsequent or earlier occurrence of the term, accordingly. You can also make use of the c and d commands to modify or erase the term in that order.

Case insensitive Search in Vim

Generally, searches are case-sensitive in the Vim editor. For example, cases searching for “KISHORE” will not match “Kishore” if the case is sensitive mode. So to avoid these kinds of problems, we have to enter the following command.

There are a few solutions to make case insensitive look in Vim editor.

Using the set ignorecase or set ic command

To make a case insensitive find, you can apply the :set ignorecase or :set ic command. This will make all following searches case insensitive mode. To make use of this instruction or command, type the following in the command line of the Vim editor.

Code example

:set ignorecase or :set ic

After entering the above command, the Vim editor will do the search related to “KISHORE” TO “Kishore” without any differences.

Using the \c modifier in Vim editor.

You may also apply the \c modifier to construct a solo search case insensitive. For instance, to look for the word “ravi” without regard to case, you would type the following:

Code example

/ravi\c

Using the smartcase setting in Vim editor.

The smartcase is more advanced than the :set ic and \c modifier command techniques. When smartcase has been switched on, results are case-insensitive whether they include any uppercase letters. Vim, for instance, will only discover occurrences of the word “Hello” in capitalized form if you set smartcase and search for it.

To access smartcase, type the following in the command line of the Vim editor:

Code example

:set smartcase

How to Search Text using Regular Expressions in Vim

Regular expressions are influential method to search for patterns of words or text.

By following the given below regular expressions you can find word or text using in Vim.

  • To find the word “Kishore” in the present buffer, you would apply the below-mentioned command or instruction.

Code Example

/kishore
  • To query for the term “hello” in the total file, you would apply the given below command or instruction.

Code Example

:g/kishore
  • To find the word phrase “Kishore” and put it back with “Ravi,” you would apply the given below command or instruction.

Code Example

:s/hello/goodbye/g
  • To look for a regular expression, you would apply the given below command or instruction.

Code example

/regex
  • For instance, to look for a word phrase that begins with “f” and ends with “e“, you would pply the given below command or instruction.

Code example

/f.*e

Here are some extra operators or commands which are useful for searching for some more difficult patterns in Vim.

  • You can apply the and n keys or commands to look for the Previous and next and match in that order.
  • In that order, you can apply the * and ? keys or operators to match zero or more or zero or one repetition of the preceding character.
  • You can apply the [] (square brackets ) characters to match a set of fonts. For example, [xyz] matches any of the characters “x“, “y“, or “z“.
  • You can apply the ^ (Caret or Hat operator) and $ (Dollar) symbols or signs to match the starting and ending of a line in that order.
  • You can use the | (vertical bar) sign or symbol to match what’s more of two patterns. For instance, /kishore\|ravi matches the word “Kishore” or the word “Ravi.”

Search and Replace Text in Vim Editor

You can use the Vim editor to search and replace the text or word by using the methods of “slash and dot” and using the “substitute command” .Here are the given below methods to search and replace text in Vim editor.

The method of using the slash and dot

The very easy solution to do a search and replace in Vim editor is utilizing the slash and dot process. We can apply the slash to find for a word, and then apply the dot to replace it.

In order to find, to search for the term or word “hello” and replace it with “hi“, we would use the following function or commands.

Code example

/article.
tutorial

This will emphasize the first appearance of the term or word “hello” and we can push the Enter button to skip to it. Then, we would enter the word “hi” and push Enter button once again. This can put back the first appearance of the word “hello” with “hi“.

The method of using the substitute command

Vim editor substitute command is used to carry out essential and advanced search and replace functions. The substitute command syntax is given below.

Code example

:substitute [range]/search_phrase/replace_phrase/[flags]

The limit [range] is not obligatory and can be applied to mention a range of lines to search and replace. The present line is the default range.

The parameter [search_phrase] is the text to look for.

The parameter [replace_phrase] is the text to replace the search word phrase with.

The parameter [flags] is not obligatory and can be apply to mention extra features for the search and replace process.

  1. In Vim, how can I look across multiple words?

    In Vim, use the search command “/” before you add the flag “v,” followed by write many words with the pipe character “|” in-between them like in what follows syntax: \v<first_wor>|<second_word>|<third_word>

  2. In Vim, how can I search for highlighted words?

    In Standard mode of Vim editor, pushing * (Shift + 8) enables Vim editor to attempt to bring up the term (or word phrase) that is now beneath the pointer. This is a great way of looking, offering that the content you would like to lookup simply includes one word.

  3. How can I leave Vim's search?

    While pushing Enter, “search mode” was already stepped out and vim’s cursor had been positioned over the very first match.

*Disclaimer: The Content we produce on this page will be only such as advice or readable information. We provide the content on the page after getting it from different sources at that time. Still, the changes from time to time, like contact information such as phone numbers, email IDs, web links and any other about that product in any form, are not responsible to us. We provide the information you need and are not an authentic source. You may refer to the original/official website/page for further clarification and more info on that product.