• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How To Search Using The Vim or Vi Text Editor

CJ Saathoff

Published on: December 28, 2023

Categories: Linux Command Line 0

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.

Table of Contents
  • Introduction
  • Basic Search Commands
    • To Find Other Instances Of A Word Using The Cursor
  • Advanced Search Options
    • To Search For Any Line Starting With A Keyword
    • To Search For Any Line Eending With A Keyword
    • To Search For An Exact Word
  • Regex Search
    • Match Any Character
    • Match Zero Or More Occurrences
    • Match One Or More Occurrences
    • Character Classes
    • Negating a Character Class
  • Search History
  • Search Configuration
    • Case Insensitive Search
    • Highlight search
  • Conclusion

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:

/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.

To Search For An Exact Word

/\<keyword\>

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

Regex Search

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:

/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.

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.

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.

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.

CJ Saathoff

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.

Related Posts

Command Substitution – Parentheses vs Backticks

Bash command substitution comes in two flavors - parentheses and backticks. Here's the difference between them.

How to Use set -e -o pipefail in Bash, and Why

While writing scripts in bash, it's important to be careful about error handling. That's what set -e -o pipefail does.

Linux find with exec: + vs ;

The "find" command is often used with the "exec" command. Here are the two options you can use, and the difference between them.

Git Bisect: A Step-by-Step Tutorial

Git bisect is a useful tool that git provides, allowing you to narrow down the commit that introduced a bug in your project.

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA