Connect with us

Website Tutorials

How To Search Using The Vim or Vi Text Editor

Published

on

How To Search Using The Vim or Vi Text Editor

Vim is an open-source text editing tool. Vim, short for “Vi Improved,” is a highly efficient and versatile text editor. Born from the ashes of the original Vi editor, Vim was created to enhance the capabilities of Vi and provide a robust, extensible, and feature-rich text editing experience. Unlike conventional text editors, the Vim editor follows a unique modal approach to editing, distinguishing between various modes for navigating, inserting, and manipulating text. This distinctive feature contributes to Vim’s steep learning curve but many users find it provides unparalleled speed and efficiency once mastered. In this article, we are going to dive into using the search functionality that is built in.

Introduction

Just imagine having a file with 5,000 or better yet 500,000 lines and being asked to find a specific word, or keyword without being able to search, it would be just like looking for a needle in a haystack. That is why most if not all text editors include search functionality, Vim is no exception to this.

To start we need to open a file that we want to search. This would be done by appending the file path after the first word in Vim, or the vi command. If you already have a file open ensure you are in normal mode by hitting the escape key before starting.

Basic Search Commands

The most basic searching functionality searches for a keyword and displays the search result on the first occurrence. To do that while in normal mode you enter a forward slash followed right after with the keyword.

For Example:

Advertisement
/keyword

That said what if you want to search backward from the bottom of a file rather than the top of the file? Then you can use a question mark and search backward starting at the bottom of the file like the below example.

?keyword

Press the n key after the search for the next occurrence or uppercase N to search in the opposite direction.

To Find Other Instances Of A Word Using The Cursor

By putting the cursor on the current word and then using * for forward search and # for backward search for the word under the cursor.

Advanced Search Options

To Search For Any Line Starting With A Keyword

/^keyword

By appending the keyword after /^ this looks for any words starting with the keyword.

To Search For Any Line Eending With A Keyword

/keyword$

By starting the command with / then the keyword and ending with $. The command looks for words ending in in the keyword.

Advertisement

To Search For An Exact Word

/\<keyword\>

\< means beginning of a word, and \> means the end of a word,

Regular expressions (regex) are powerful tools for searching and manipulating text. Here are some basic regex examples for Vim search:

Match Any Character

The dot (.) in a regex pattern matches any single character string:

/col.r

This will match “color,” “colour,” and any other five-letter word with “col,” followed by any character, and then “r.”

Match Zero Or More Occurrences

The asterisk (*) matches zero or more occurrences of the preceding character or group:

Advertisement
/ab*c

This will match “ac,” “abc,” “abbc,” and so on.

Match One Or More Occurrences

The plus sign (+) matches one or more occurrences of the preceding character or group:

/ab+c

This will match “abc,” “abbc,” and so on, but not “ac.”

Character Classes

Use square brackets to define a character class:

/[aeiou]

This will match any vowel.

Advertisement

Negating a Character Class

To match any character except those in a character class, use ^ within the square brackets:

/[^0-9]

This will match any non-digit character.

Search History

Vim keeps a limited search history that can be accessed while in normal mode which will bring up a section where you can use the arrow keys to re-run a search command inside of vim.

:history /
Vim command :history /

Search Configuration

Let’s touch on a few configuration options related to searching that are configured in Vim that you may find useful in your search.

Case Insensitive Search

By default, searching is case sensitive, if you want to change the search to case insensitive search there a a few ways of doing so.

For one search is to append \c after the search pattern. For reference, the reverse is Uppercase \C after the pattern forces case match search.

Advertisement

You can set it while working on a specific text file with the following command:

:set ignorecase

To reverse that append so it looks like:

:set noignorecase

To permanently set this you can also add the setting you want to ~/.vimrc file

Highlight search

If you want to enable highlighting of the search term you can set use:

:set hlsearch

This can also be permanently set via the ~/.vimrc file.

Advertisement

For reference to disable use nohlsearch or remove the line from the ~/.vimrc file.

To confirm if this worked search again and if highlights a match like below then you know it worked.

Highlight example

Conclusion

In conclusion, mastering the search functionality in Vim is essential for efficiently navigating and editing text, especially when dealing with large files. Vim’s modal approach to editing, though initially challenging, proves to be a powerful and efficient method once learned. Basic search commands using forward slash and question marks allow users to quickly locate keywords, and advanced options, such as regex searches and word-boundary matching, provide a more refined search experience.

Understanding how to navigate search history and configure search options further enhances the Vim user’s experience. The ability to make searches case-insensitive or enable highlighting adds flexibility to the search process. Vim’s search capabilities, coupled with its extensibility and robust features, make it a versatile tool for developers, sysadmins, and anyone working with text files.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.