How to Search In Vim / Vi [Vim Tutorial]

How to Search In Vim / Vi [Vim Tutorial]

Vim is a popular open-source Unix text editor. In this article, we will discuss how to perform search operations in Vim / Vi.

How to Search in Vim / Vi [Vim Tutorial]

Before jumping into the main content, let’s have a look into the command set in Vim.

Editing Text Using Vim

  • cc – replace an entire line
  • C / c$ – replace from the cursor to the end of a line
  • cw – replace from the cursor to the end of a word
  • J – merge the line below to the current one with a space in between them
  • gJ – merge the line below to the current one without any space in between them
  • r – replace a single character
  • s – delete a character
  • u – undo
  • Ctrl + r – redo

Search In File Using Vim

  • * – jump to the next instance of the current word
  • # – jump to the previous instance of the current word
  • /pattern – Use this to search forward for the specified pattern
  • ?pattern – Use this to search backward for the specified pattern
  • n – repeat the search in the same direction
  • N – repeat the search in the opposite direction

Now, let’s have a step-by-step guide to search file in Vim or Vi editor. You must be in normal mode if you want to search in Vim. Usually, When you launch the Vim editor, you’re in the normal mode and if you want to go back to the normal mode from any other state then you can just do it by pressing the Esc key.

READ More Relevant Stuff:  How To Set Up Automatic Updates On Ubuntu 22.04 LTS

Basic steps for the search in a file using Vim:

  1. Press /.
  2. Type the search pattern.
  3. Press Enter to perform the search.
  4. Press n to find the next occurrence or N to find the previous occurrence.

If you want to search for a whole word, then you can do it by /\<search pattern \>. The search result is case-sensitive in Vim. Vim keeps track of all the search operations. if you want To see the search history, press / or ? and use the arrow up/down keys to find a previous search history.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *