How To Search In VI Editor - Fedingo

VI editor is a popular text editor available in every Linux distribution. If offers a wide range of features and is very easy to use. While using vi editor, you may need to find specific word in vi editor or search a sentence in vi editor. In this article, we will look at how to search in vi editor. You can use the same steps for vim editor also.

How To Search in VI Editor

There are many use cases for search in vi editor. We will look at each of them one by one.

Here is a quick overview of how search in vi editor works.

  1. VI editor allows you to search forwards or backwards.
  2. When you are in vi editor, type forward slash ‘/’ and type your search string to search forward
  3. When you are in vi editor, type forward slash ‘?’ and type your search string to search backward
  4. Once a word is found, type n to find the next occurrence going forward, and type N to find the next occurrence while going backward.

Also read : How to set default text editor in linux

Find word in vi editor

Here is how to search for a word forwards in vi/vim editor. Open your file in vi editor.

$ sudo vi test.txt

Press Esc key. Type / and then your search string.

For example, type /product in vi editor to search for “product”. Vi will immediately take your cursor to the first occurrence of your search string. Type n to go to the next occurrence forwards.

Also read : How to Undo Git Add before commit

Search for word backwards

Here is how to search for a word backwards in vi/vim editor. Open your file in vi editor.

$ sudo vi test.txt

Press Esc key. Type ? and then your search string.

For example, type ?product to search for “product”. Vi will immediately take your cursor to the last occurrence of your search string. Type N to go to the next occurrence backwards.

Also read : How to Install AWS CLI in Ubuntu

Search for present word

Sometimes you may want to search for the present word on which your cursor is currently located. Simply take your cursor to the word you want to search and type *, and vi editor will directly take you to the next occurrence of the search string going forward.

Similarly, if you take your cursor to the word you want to search and type # then vi editor will take you to the last occurrence of the search string going forward.

Also read : How to Tar a file in Linux

Search for a function in vi editor

Many times we need to search for a specific function name in our file. In such cases you can directly run the following command from your command line, without opening vi editor.

$ sudo vi +/function_name filename

For example, here is the command to search for function name print_output() in test.txt file.

$ sudo vi +/print_output test.txt

Also read : How to Upgrade PHP Version in Ubuntu

Search for a line number in vi editor

Sometimes we need to search for a specific line number in our file. In such cases you can directly run the following command from your command line, without opening vi editor.

$ sudo vi +line_number filename

For example, here is the command to search for line number 20 in test.txt file.

$ sudo vi +20 test.txt

Also read : How to list all services in linux using systemctl

Search for whole word

Sometimes you may need to search for a term with whitespace characters in them, or you want to do an exact match of your search string. If you want to search the exact word in a file use \< and \> to enclose it. Here is an example to search the word “sample” in our file.

/\<sample\>

Alternatively, you can take the cursor to that word and hit *.

Case sensitive search

If you want to do a case sensitive search in vi editor enclose it in \< and \> and add \C after it. Here is an example to do a case sensitive search for “SAMPLE” in our file.

/\<SAMPLE\>\C

Also read : How to Configure FTP Server in Ubuntu

Case insensitive search

If you want to do a case insensitive search in vi editor enclose it in \< and \> and add \c after it. Here is an example to do a case insensitive search for “sample” in our file.

/\<SAMPLE\>\c

As you can see, it is very easy to search for strings in vi editor.

Also read : How to Install git in Ubuntu How to Install virtualenv in Ubuntu

Related posts:

How to Create Password Protected Zip File in LinuxHow to Disable Commands in LinuxHow to Schedule Shutdown in Ubuntu LinuxHow to Store File Content in Shell VariableHow to Fix Unacceptable TLS Certificate Error in LinuxHow to Enable IP Forwarding in UbuntuHow to Write to File in BashHow to Prevent Accidental File DeletionSreeram Sreenivasan

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.

Từ khóa » Vi In Linux Search