• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » How to search in VIM

How to search in VIM

September 21, 2020 - by Zsolt Agoston - last edited on September 23, 2020

 1. Intro

It's been almost 30 years since the Visual Improved editor (VIM in short) was born and it's still one of the most popular text editors on Unix type operating systems. While it has the most simple and clean interface it's proven quite difficult for first time users to find their way around it's usage.

Legend has it a dev once said: "I've been using vim for 2 years now, mostly because I cannot figure out how to exit it!"

In this article we walk through the ways how to search certain words, or strings in any text file.

2. Open VIM

As always, we use simple examples to go through the process. In my experience that makes it easier to understand the logic behind the described procedures. First, we open the ssh server configuration file that is most likely already available even on very basic linux, bsd or mac installations.

[root@localhost ~]# vim /etc/ssh/sshd_config

If you receive an error message saying vim is not a valid command, you'll need to install the proper VIM package. See details under the 'TIPS' section below! You might use the vi command instead, but that one is very limited as it only contains basic functions.

3. How to Search

Always use these commands in normal mode. When you open the editor it automatically starts up in normal mode, which is designed for command execution. You can type, delete text in insert mode. To get into that mode simply push the "i" key on your keyboard. To switch back to normal mode, push the ESC key.

As a rule of thumb, if unsure, always puch the ESC key so you make sure you are in the right mode!

# Find the string 'fox' forward from the actual position:
/fox
How to search in VIM
# Find the string 'fox' backward from the actual position:
?fox
How to search in VIM
# Highlight results
:set hlsearch

# To disable it
:set nohlsearch
How to search in VIM
# Case insensitive search, that will include 'fox' and also 'Fox':
:set ignorecase
How to search in VIM
# Search for the exact word: fox. That means 'SuperFox' is excluded, as the 'fox' string is not a separate word
/\<fox\>
How to search in VIM
# To get the search history use the following keys. You can navigate in the list with the arrow keys (or j, k), and use ENTER to do repeat the search!
q/
How to search in VIM
# Tip: search for the word at the actual cursor position:
Forward search: *
Backward search #

4. TIPS

VI vs VIM

Vi is the old 'VIsual' editor, while VIM stands for it's successor, the 'Visual IMproved' app.

On Debian/Ubuntu based system the vi command opens the same full-fledged VIM application, so vi and vim are interchangeable there. However, be aware that on RHEL/CentOS machines the vi command is mapped to the old, dumb version of the command, while vim opens the full application up that we need.

Generally speaking, most of the linux distributions contain both (if you haven't already, install the improved version on your box)

#On Debian/Ubuntu:
apt-get install vim

# On RHEL./CentOS:
yum install vim

Reader Interactions

Comments Cancel reply

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

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  •  1. Intro
  • 2. Open VIM
  • 3. How to Search
  • 4. TIPS

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}