Linux logo

A Little About Vim and How to Search in it

Vim is not just your average text editor; it’s the ninja of text editing. Whether you’re coming from the world of Notepad, Sublime Text, Atom, Nano, or any other text editor, Vim is the ultimate tool for composing and tweaking your text game.

What sets Vim apart and gives it the cool factor?

  1. Efficiency: Vim is the efficiency maestro. It’s not here to play; it’s here to get things done. With Vim, you’re not just editing text; you’re editing text at the speed of thought.
  2. Ubiquity: Vim is the James Bond of text editors—it’s everywhere. No matter if you’re rocking a Mac, rolling with Windows, or cruising through a Linux landscape, Vim is there, ready to unleash its text-editing prowess. And if your daily grind involves terminal sessions, Vim is not just an option; it’s the only option.

Now, let’s dive into the art of searching in the Vim text editor. Get ready to unleash your inner text-editing ninja! 🕵️‍♂️💻

How to search in Vim/vi editor

A few points to note regarding searching in Vim/vi:

  • In normal mode, you can search forward or backward.
  • You can search forward in vim/vi by pressing / and then typing your search pattern/word.
  • Now as soon as you find the occurrence of this word. Then you can press N to find the next occurrence of this word in the forward direction.
  • Otherwise, If you would like to search backward then press ? And type the text you wish to search followed by entering the key.
  • Once a word found in vim, you can press the n key to go right to the next occurrence of this word in backward.
An example of searching in vim

In this example, we will search a Word in Vim from the forward direction

In this case, we’re going to search the word “term” within the document /etc/searchtxt.

First, open /etc/searchtxt file with the following command:

vim /etc/searchtxt

Now press the Esc key to come in the normal manner.

Then press / key and input the word term which we want to search and hit Enter:

/term

It should highlight all occurrences of “term”.

Further Reading:  What is tar Command in Linux?

Now you may press N crucial to find the next occurrence in the forward direction. If you want to go backward, press ‘n’ key/

How to Look for the current word

The term where the cursor is located known as the current word. To find current word follow below steps:

Move the cursor on the word which you would like to search.

Now press * to search the term in the forward direction. You could even get another occurrence by pressing on the * key.

You may press # to search the term in the backward direction.

Case insensitive search in Vim

We can also conduct case insensitive search in Vim with the following two config choices. For Instance, ignore case in search patterns, type:

:set ignorecase

Now search for words in different cases.

Another option is to override the ‘ignorecase’ option if the search pattern contains upper case characters.

:set smartcase "
Search a Word in Vim at the backward example

In this case, we’re going to search word “term” within the document /etc/searchtxt.

First, open /etc/searchtxt file with the following command:

vim /etc/searchtxt

Now press Esc key to come in normal mode.

Then press ? Key and enter the word “term” that we would like to search and press Enter:

?term

You can press n essential to find the next occurrence in the backward direction.

How to Highlight the Words When Search

You can enable highlight words when searching by using the following command.

:set hlsearch

To Quit highlighting phrases when search, enter the following command:

:set !hlsearch
How do I search only for “word”

A use case is how you will search phrase in vim with /word. How can I search just for test, excluding searches for test1 and test2?

Use this command

/\<test\>

If You’re working in Ubuntu, follow the steps:

Press / and type “test” to search

To search ahead press ‘SHIFT‘ key with * key

To search in backward press ‘SHIFT‘ key with # key

Command to search and replace

You may search the whole file and replace the term by using this command:

:%s/search_term/replaceterm/

To confirm before replacement, use this command

:%s/search_term/replace_term/c

Common Questions Regarding Understanding Vim and Searching

About Vim’s Uniqueness

Q1: What sets Vim apart from other text editors?

Vim stands out for its efficiency and ubiquity. It is designed for users seeking a powerful and efficient text editing experience.

Q2: How does Vim prioritize efficiency?

Efficiency is a core focus of Vim. It streamlines text editing through various approaches, ensuring a swift and seamless editing experience.

Q3: What is the cool factor of Vim’s ubiquity?

Further Reading:  What is Linux ls command?

Vim is available on major platforms, including Mac, Windows, and Linux distributions. It’s particularly handy for users working in ssh’ed terminal sessions.

Searching in Vim

Q4: How do I search in Vim?

In normal mode, use / followed by your search pattern for forward search, and ? for backward search. Navigate occurrences with N and n, respectively.

Q5: Can I search for the current word in Vim?

Yes, move the cursor to the desired word and press * for forward search or # for backward search.

Q6: How does case-insensitive search work in Vim?

Enable case-insensitive search with :set ignorecase. To override this setting for specific patterns with uppercase characters, use :set smartcase.

Q7: How can I highlight words while searching in Vim?

Enable word highlighting with :set hlsearch. To stop highlighting, use :set nohlsearch.

Advanced Searching Techniques

Q8: How can I search exclusively for a specific word like “test” in Vim?

Use the command /\<test\> to search for the exact word “test,” excluding variations like “test1” or “test2.”

Q9: What is the command for search and replace in Vim?

Use :%s/search_term/replaceterm/ to search the entire file and replace a term. For confirmation before replacement, use :%s/search_term/replace_term/c.

Q10: How do I perform a case-sensitive search in Vim?

By default, Vim is case-sensitive in its search. If you want to perform a case-sensitive search, ensure that the pattern you input matches the case exactly.

Wrapping Up 

In conclusion, Vim stands out as a text editor due to its emphasis on efficiency and ubiquity. Its focus on streamlining text editing processes makes it a powerful tool for users across various platforms, including Mac, Windows, and Linux distributions. Notably, for those working in ssh’ed terminal sessions, Vim remains the sole text editor available.This tutorial delved into the art of searching within Vim, highlighting key points for effective navigation:
  1. Searching Basics:
    • In normal mode, users can search forward or backward using / or ?.
    • Press N to find the next occurrence in the forward direction or n to go backward.
  2. Practical Example:
    • Demonstrated a search for the word “term” in the document /etc/searchtxt.
    • Illustrated the process of opening the file, entering normal mode, initiating the search, and navigating occurrences.
  3. Searching for the Current Word:
    • Explained how to find the current word by moving the cursor and using * for forward search or # for backward search.
  4. Case-Insensitive Search:
    • Introduced case-insensitive search with :set ignorecase.
    • Explored an option to override case sensitivity for specific patterns using :set smartcase.
  5. Backward Search Example:
    • Provided an example of searching the word “term” backward in /etc/searchtxt.
    • Illustrated the steps, including opening the file, entering normal mode, initiating the search, and navigating occurrences.
  6. Highlighting Words During Search:
    • Enabled word highlighting during search with :set hlsearch.
    • Introduced the command :set !hlsearch to stop highlighting.
  7. Exclusive Word Search:
    • Addressed a specific use case by demonstrating how to search exclusively for the word “test” using /\<test\>.
  8. Search and Replace Command:
    • Explained the command :%s/search_term/replaceterm/ for searching and replacing throughout the file.
    • Introduced a confirmation option with :%s/search_term/replace_term/c.

This comprehensive tutorial equips users with the knowledge to efficiently search within Vim, enhancing their text editing capabilities.

Further Reading:  Know Which Linux Version You Are using: 5 Ways