I can search word in vim with /word
. How can I search only for word
, excluding searches for word1
and word2
?
Freedom_Ben
11k10 gold badges67 silver badges88 bronze badges
asked Jan 19, 2009 at 19:49
1
like this:
/<word>
<
means beginning of a word, and >
means the end of a word,
Adding @Roe’s comment:
VIM provides a shortcut for this. If you already have word on screen and you want to find other instances of it, you can put the cursor on the word and press '*'
to search forward in the file or '#'
to search backwards.
answered Jan 19, 2009 at 19:51
Nathan FellmanNathan Fellman
121k99 gold badges258 silver badges319 bronze badges
8
- vim filename
- press /
- type word which you want to search
- press Enter
answered Dec 14, 2015 at 10:01
MushirMushir
1,2139 silver badges12 bronze badges
3
If you are working in Ubuntu,follow the steps:
- Press
/
and type word to search - To search in forward press ‘SHIFT’ key with
*
key - To search in backward press ‘SHIFT’ key with
#
key
SOFe
7,8114 gold badges32 silver badges60 bronze badges
answered Aug 11, 2016 at 7:11
Priya RPriya R
4834 silver badges2 bronze badges
4
For basic searching:
- /pattern — search forward for pattern
- ?pattern — search backward
- n — repeat forward search
- N — repeat backward
Some variables you might want to set:
- :set ignorecase — case insensitive
- :set smartcase — use case if any caps used
- :set incsearch — show match as search
answered Jan 5, 2018 at 11:36
NeeruKSinghNeeruKSingh
1,5453 gold badges21 silver badges26 bronze badges
1
Basic search (once you have opened the file in vim
) using vim
:
-Hit ESC
on your computer keyboard
-Hit the forward slash symbol on your keyboard /
-Type the word or symbol you intend to search; for example, to search for the term «rbmq» type /rbmq
-Hit Enter
-Hit n to search forward (moving towards the end of the file) and N to search backward (moving towards the beginning of the file)
answered Jul 22, 2021 at 19:10
AsifAsif
6441 gold badge7 silver badges13 bronze badges
1
This tip shows how to search using Vim, including use of *
(the super star) to search for the current word. Search options are described, and the see also section links to other useful searching tips.
Basic searching[]
In normal mode you can search forwards by pressing /
(or <kDivide>
) then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press n
to search forwards for the next occurrence, or N
to search backwards. Type ggn
to jump to the first match, or GN
to jump to the last.
Note: ggn
skips first match if it is at row 1 column 1. Type Gn
to jump to the real first match. For this, 'wrapscan'
must be on (default).
Search backwards by pressing ?
then typing your search pattern. Pressing n
searches in the same direction (backwards), while N
searches in the opposite direction (forwards).
Searching for the current word[]
In normal mode, move the cursor to any word. Press *
to search forwards for the next occurrence of that word, or press #
to search backwards.
Using *
(also <kMultiply>, <S-LeftMouse>) or #
(also <S-RightMouse>) searches for the exact word at the cursor (searching for rain would not find rainbow).
Use g*
or g#
if you don’t want to search for the exact word.
Using the mouse[]
With appropriate settings, you can search for an exact word using the mouse: Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.
This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your vimrc to enable mouse searches:
:set mousemodel=extend
In gvim, click the Edit menu, then Global Settings, then the «tear off» bar at the top. That will show a floating Global Settings menu with useful Toggle Pattern Highlight and Toggle Ignore-case commands.
More searching[]
Vim maintains a search history. Type /
or ?
and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.
Suppose the cursor is on a word, and you want to search for a similar word.
Press /
then Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter. Use Ctrl-r Ctrl-w for a <cword>, or Ctrl-r Ctrl-a for a <cWORD>.
After searching, press Ctrl-o to jump back to your previous position (then Ctrl-i will jump forwards).
After searching, an empty search pattern will repeat the last search. This works with /
, :s
and :g
.
So, after searching for a word, use :%s//new/g
to change all occurrences to ‘new’, or :g/
to list all lines containing the word. See substitute last search.
You can enter a count before a search. For example 3/pattern
will search for the third occurrence of pattern, and 3*
will search for the third occurrence of the current word.
You can highlight all search matches (and quickly turn highlighting off), and you can use a mapping to highlight all occurrences of the current word without moving (see highlight matches).
A search can include an offset to position the cursor. For example, searching with /green
positions the cursor at the beginning of the next «green», while /green/e
positions the cursor at the end of the next «green». :help search-offset
A search pattern can include characters with codes specified in decimal or hex. For example, searching with /%d65
or with /%x41
both find ‘A
‘ (character decimal 65 = hex 41), and searching with /%d8211
or with /%u2013
both find ‘–
‘ (a Unicode en dash). :help /%d
Case sensitivity[]
By default, searching is case sensitive (searching for «the» will not find «The»).
With the following in your vimrc (or entered via a toggle mapping), searching is not case sensitive:
:set ignorecase
Now the command /the
will find «the» or «The» or «THE» etc. You can use c
to force a pattern to be case insensitive, or C
to force a pattern to be case sensitive. For example, the search /thec
is always case insensitive, and /theC
is always case sensitive, regardless of the 'ignorecase'
option.
If 'ignorecase'
is on, you may also want:
:set smartcase
When 'ignorecase'
and 'smartcase'
are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, /The
would find only «The», while /the
would find «the» or «The» etc.
The 'smartcase'
option only applies to search patterns that you type; it does not apply to *
or #
or gd
. If you press *
to search for a word, you can make 'smartcase'
apply by pressing /
then up arrow then Enter (to repeat the search from history).
When programming, there is generally no reason to want 'smartcase'
to apply when you press *
. For other situations, use:
:nnoremap * /<<C-R>=expand('<cword>')<CR>><CR> :nnoremap # ?<<C-R>=expand('<cword>')<CR>><CR>
With these mappings, if 'smartcase'
is on and you press *
while on the word «The», you will only find «The» (case sensitive), but if you press *
while on the word «the», the search will not be case sensitive.
The mapping for *
uses /
to start a search; the pattern begins with <
and ends with >
so only whole words are found; <C-R>=
inserts the expression register to evaluate expand('<cword>')
which inserts the current word (similar to Ctrl-R Ctrl-W but avoiding an error when used on a blank line).
Show the next match while entering a search[]
To move the cursor to the matched string, while typing the search pattern, set the following option in your vimrc:
:set incsearch
Complete the search by pressing Enter, or cancel by pressing Esc. When typing the search pattern, press Ctrl-L (:help c_CTRL-L) to insert the next character from the match or press Ctrl-R Ctrl-W (:help c_CTRL-R_CTRL-F) to complete the current matching word.
Other search options[]
By default, the 'wrapscan'
option is on, which means that when «search next» reaches end of file, it wraps around to the beginning, and when «search previous» reaches the beginning, it wraps around to the end.
These examples show how to set 'wrapscan'
(abbreviated as 'ws'
):
:set nowrapscan " do not wrap around :set wrapscan " wrap around :set wrapscan! " toggle wrap around on/off :set ws! ws? " toggle and show value
By default, search hits may occur in lines at the top or bottom of the window. The 'scrolloff'
option controls whether context lines will be visible above and below the line containing the search hit.
If your text is folded, you probably want folds to automatically open to reveal search hits. To achieve that, the 'foldopen'
option should include «search» (check by entering :set fdo?). :help ‘foldopen’ Conversely, this option can be used to Search only in unfolded text.
See also[]
- Searching
- Highlighting search matches
- Search for visually selected text
- Search patterns regex tutorial with useful searches
- Search for current word in new window
- Make search results appear in the middle of the screen
- Search and replace using
:s
to substitute text - Browse the searching category for more.
- Searching in multiple files
- Find in files within Vim
- Finding a file from its name
- Project browsing using find
- Find files in subdirectories
References[]
- :help *
- :help <S-LeftMouse>
- :help :<cword>
- :help jump-motions
- :help ‘iskeyword’ controls which characters
*
considers make a word
[]
On UK keyboards, <Shift-3> produces ₤ but works just like #
for searching backwards.
Introduction
Vim and its older counterpart Vi are widely used text editors. They are both open-source (free) and available on most Linux and macOS distributions.
Searching in Vim/Vi for words or patterns is a common task that allows you to navigate through large files easily.
This tutorial shows you how to search in Vim/Vi with practical examples.
Important: Make sure you are in Normal Mode before you start searching. This allows in-text navigating while using normal editor commands. Vi and Vim open a file in normal mode by default.
To switch to normal mode, press Esc.
Basic Searching in Vim / Vi
There are two ways to search for a pattern of numbers or letters in the Vim/Vi text editor.
1. Search forward for the specified pattern
2. Search backward for the specified pattern
The direction is determined in relation to the cursor position.
Searching Forward For Next Result of a Word
To search forward, use the command:
/pattern
Replace pattern
with the item(s) you want to find.
For example, to search for all instances of the pattern «root», you would:
1. Press Esc to make sure Vim/Vi is in normal mode.
2. Type the command:
/root
The text editor highlights the first instance of the pattern after the cursor. In the image below, you can see that the result is part of a word that contains the specified pattern. This is because the command does not specify to search for whole words.
From this position, select Enter and:
- jump to the next instance of the patter in the same direction with
n
- skip to the next instance of the pattern in the opposite direction with
N
Searching Backward For a Word
To search backward type:
?pattern
Replace pattern
with the item(s) you want to find.
For example, to search for all instances of the pattern «nologin», you would:
1. Press Esc to make sure Vim/Vi is in normal mode.
2. Type the command:
?nologin
Vim/Vi highlights the first instance of the specified pattern before the cursor.
Hit Enter to confirm the search. Then, you can:
- move to the next instance of the pattern in the same direction with
n
- jump to the next instance of the pattern in the opposite direction with
N
Searching for Current Word
The current word is the word where the cursor is located.
Instead of specifying the pattern and prompting Vim/Vi to find it, move the cursor to a word, and instruct it to find the next instance of that word.
1. First, make sure you are in normal mode by pressing Esc.
2. Then, move the cursor to the wanted word.
3. From here, you can:
- Search for the next instance of the word with
*
- Search for the previous instance of the word with
#
Each time you press *
or #
the cursor moves to the next/previous instance.
Searching for Whole Words
To search for whole words, use the command:
/<word>
The text editor only highlights the first whole word precisely as specified in the command.
Open a File at a Specific Word
Vim (and Vi) can open files at a specified word allowing you to skip a step and go directly to the searched term.
To open a file at a specific word, use the command:
vim +/word [file_name]
or
vi +/word [file_name]
For example, to open the /etc/passwd file where it first uses the term «root», use the command:
vim +/root /etc/passwd
The text editor opens the file and the first line it displays is the one containing the term «root», as in the image below.
Case Insensitive Search
By default Vi(m) is case sensitive when searching within the file. For example, if you search for the term /user
it doesn’t show results with the capitalized U (i.e., User).
There are several ways to make Vim/Vi case insensitive.
- To search for a specified word with the case insensitive attribute, run the command:
/<word>c
The c
attribute instructs Vi(m) to ignore case and display all results.
- An alternative is to set Vim to ignore case during the entire session for all searches. To do so, run the command:
:set ignorecase
- Lastly, the configuration file can be modified for the text editor to ignore case permanently. Open the ~/.vimrc file and add the line
:set ignorecase
.
Highlight Search Results
Vi(m) has a useful feature that highlights search results in the file. To enable highlighting, type the following command in the text editor:
:set hlsearch
To disable this feature run:
:set !hlsearch
Search History
Vi(m) keeps track of search commands used in the session. Browse through previously used commands by typing ?
or /
and using the up and down keys.
Conclusion
After reading this article, you have multiple options to search and find words using Vim.
Once you find the searched term, you can move on to cutting, copying, or pasting text.
Linux is a versatile operating system that allows you to perform a standard Vi word search using different keys on your computer keyboard. This tutorial will list the different keys and functions that you can use to search and find words in Vi/Vim in Linux.
How to find a word in Vi / Vim?
- To find a word in Vi/Vim, simply type the / or ? key, followed by the word you’re searching for.
- Once found, you can press the n key to go directly to the next occurrence of the word.
- Vi/Vim also allows you to launch a search on the word over which your cursor is positioned. To do this, place the cursor over the term, and then press * or # to look it up.
Need more help with Linux? Check out our Forum!
Text editors like How to Search to Find a Word in Vim or Vi Text Editor? and its predecessor Vi are extensively used. They may be found on the majority of Linux and macOS distributions and are both open-source (free).
Finding terms or patterns to search for in Vim/Vi is a typical activity that makes it simple to browse through huge files.
This lesson uses real-world examples to demonstrate how to search in Vim/Vi.
Basic Vim/Vi Searching
In the Vim/Vi text editor, there are two ways to look for a pattern of numbers or letters.
- Look ahead for the desired pattern
- Look for the specified pattern going backwards
In relation to the cursor point, the direction is decided.
Looking for A Word’s Next Result in the Future
Use the command: to advance the search.
/pattern
Put the item(s) you want to find in lieu of pattern.
For instance, to find all occurrences of the pattern “root,” you would:
1: To make sure Vim/Vi is in regular mode, use Esc.
2: Enter this command:
/root
The first occurrence of the pattern after the cursor is highlighted in the text editor. You can see that the outcome is a word’s worth of text that includes the desired pattern in the image below.
This is so because searching for complete words is not specified by the command.
Choose Enter from this position, then:
- N will advance you to the following instance of the pattern in the same direction
- N will advance you to the following instance of the pattern in the opposite direction.
Looking for a Word in Backward
How to Search to Find a Word in Vim or Vi Text Editor? Type the following in the search box to go backward:
? pattern
Put the item(s) you want to find in lieu of pattern.
For instance, if you wanted to find every instance of the pattern “nologin,” you would:
1: To make sure Vim/Vi is in regular mode, use Esc.
2: Enter this command:
? nologin
Before the cursor, Vim/Vi highlights the first occurrence of the chosen pattern.
To confirm the search, press Enter. So you can:
- go to the following occurrence of the pattern while keeping n constant.
- hop with N to the following pattern instance in the opposite way.
Looking for a New Word
The word where the cursor is situated is the current word.
Move the cursor to a word and tell Vim/Vi to look for the next instance of that word rather than describing the pattern and asking it to find it.
- Check that normal mode is selected by hitting Esc.
- Next, position the pointer over the desired word.
- You can: from here
- Use * to find the following occurrence of the word.
- Look for the word’s prior occurrence using #
Each time you press * or # the cursor moves to the next/previous instance.
Searching for Whole Words
How to Search to Find a Word in Vim or Vi Text Editor? To search for whole words, use the command:
/
The text editor only highlights the first whole word precisely as specified in the command.
Open a File at a Specific Word
Vim (and Vi) can open files at a specified word allowing you to skip a step and go directly to the searched term.
To open a file at a specific word, use the command:
vim +/word [file_name]
or
vi +/word [filename]
For example, to open the /etc/passed file where it first uses the term “root”, use the command:
vim +/root /etc/passed
The first line that the text editor shows when it opens the file and displays the word “root” is shown in the illustration below.
Case Ignorant Search
When searching within the file, Vi(m) defaults to treating case differently. For instance, when you search for the term “user,” results without the capital U are not displayed (i.e., User).
Case insensitivity can be achieved in Vim/Vi in a number of ways.
Use the following command to conduct a case-insensitive search for a certain word:
/<word>c
How to Search to Find a Word in Vim or Vi Text Editor? Vi(m) is told to ignore case and display all results thanks to the c property.
Vim can be configured to ignore case for all searches across the entire session as an alternative. Run the following command to do so:
: set disregard case
The configuration file can also be changed to permanently make the text editor disregard case. Add the line: set ignorecase to the. vimrc file by opening it.
Featured Search Results
The nice feature in Vi(m) that emphasises search results in the file is quite helpful. Enter the following command in the text editor to enable highlighting:
hlsearch set
Run: to turn off this functionality.
set to "hlsearch"
Inquiry History
The search commands used throughout the session are recorded by Vi(m). Use the up and down keys along with the characters? or / to navigate through previously used commands.
Conclusion
After reading this tutorial How to Search to Find a Word in Vim or Vi Text Editor? you have many possibilities for using Vim to search for and find words.
Once the search term has been located, you can continue by text-cutting, copying, or pasting.
29 Mar, 22
by Susith Nonis
5 min Read
List of content you will read in this article:
- 1. How to Search in Vim Editor?[Different Ways]
- 2. Conclusion
Vim, a sophisticated and highly customizable command-line program, is a text editor that enables quicker text editing. Vim was invented by Bram Moolenaar. Depending on our needs, we may make use of the different plugins available in Vim. As in macOS and most of the best Linux distros, VI Editor comes Preinstalled. One of the most common tasks for everyone is Finding/Searching a text in Vim/VI Editor while working with files. As this is very basic information, So working and boosting your productivity with such basic Vi commands will help you. this article gives you a brief about Vim Search/Vim Find/Vi Search/Vi Find.
VIM, or Visual Editor for Unix-like Operating Systems, provides the greatest command-line text file editing experience for Unix. Vim is a revamped version of recent operating systems’ popular vi text editor. This is a text editor with a lot of options.
How to Search in Vim Editor?[Different Ways]
1. Simple Vim Search
Users must be in normal mode to search in Vim. When you open the Vim editor, you’re in this mode and hit the Esc key to return to regular mode from any other mode.
You can quickly discover text using the Vim’s//forward slash and?’ question mark. Press ‘/’ to search for the forward searches, and click ‘?’ for backward searches. Input the search pattern and click Enter to search.
The following are the fundamental stages for searching in Vim:
- Press the button of / key.
- In the box, type the criteria for the scan.
- Click Enter to commence the search.
- Press N to identify the next incident, then press N to discover the previous incident.
For example,
Open file1 in the vim editor
$ vim /file1
Search for a word named «Hotel» in the forward direction:
- Press ESC key
- Type /Hotel
- Hit n to search forwards for the next occurrence of a word named «Simran». You can press N to search backward.
2. Look for the Entire Word
To find out the complete word, start your search by pressing / or ?, type < to mark the starting of a required word, enter the search pattern, type > to mark the endpoint of the word, and hit Enter to perform the search.
For example, to find «Linux,» you might type /<Linux>:
3. Specify a Word to Open a File at That Word
Vim/Vi can open files at a certain word, allowing you to skip a step and get straight to the phrase you’re looking for.
Use the following mentioned command for Vim find.
vim +/word [filename]
For example, to access the /d1/f1 file where the phrase «xyz» appears for the first time, run the command:
vim +/xyz /d1/f1
4. Find in Vim Without Regarding for Case Sensitivity
When looking inside a file, Vim is case-sensitive by definition. For example, when you search for the word /Linux, no results with the uppercase L appear as «Linux».
There are several techniques to accomplish case insensitivity in Vim.
Use the following Linux command to search for a specific term using the case insensitive attribute:
/<word>c
5. Results from Your Search Should be Highlighted
Vim provides a handy function that emphasizes the file’s search results. In the text editor, run the following command to activate highlighting:
: set hlsearch
6. Vim Search History
Vim maintains track of all the searches you’ve done so far in this session. Use the arrow keys to discover a prior search operation, hit / or? and move through the search history. Hit Enter to start the search. Before executing the procedure, you may also change the search pattern.
7. Look up the Current Word
You may also find a full word by dragging the cursor over it and hitting * (asterisk) to search forwards or # (hash) to look backward. Press * or # once more to discover the next matches.
For example,
Let’s say you open the following file:
$ cat file.txt
Place the cursor on any word in normal mode, such as 168. To search for the next occurrence of the word 168, press * or # to search backward.
Conclusion
Hence in this article, we look at how to search in Vim using the forward Slash ‘/’ and question mark ‘?’. Also, various other search techniques may help us easily find the specific search criteria or pattern in our document. Hence using these methods, you can easily find in Vim editor and get what you want. Vim is really user-friendly and very easy to use for the developers, and the search command can accelerate the search tasks that have to be performed in the Vim editor. Hence, this tutorial helped you understand the Vim editor’s search functionality.
People Are Also Reading:
- How To Select All In Vim/Vi Editor?
- Vim/Vi editor vs Nano editor
- VIM Cheat Sheet PDF for quick reference
- Vim Editor shortcuts
- Top Linux VIM Commands with Examples
- VIM Save and Exit
I am a new Linux and Unix-like operating system user. How do search and find words in Vi Vim? How can I find a Word in Vim or vi text editor?
Introduction: Vi and vim is a text editor for Linux, macOS, Unix, and *BSD family of operating systems. Vim is a free and open source text editor. One can search using various vi text editor keys. This page shows how to search and find words in vi or vim text editor running on a Linux or Unix-like systems.
Find a word in Vim or vi text editor
To search using Vim/vi, for the current word:
- In normal mode, you can search forward or backward.
- One can search forward in vim/vi by pressing / and then typing your search pattern/word.
- To search backward in vi/vim by pressing ? and then typing your search pattern/word.
- Once word found in vim, you can press the n key to go directly to the next occurrence of the word in backwards. Press the N key to go directly to the opposite direction, i.e. forwards.
Let us see some examples.
Searching for words in vim/vi
Let us open a file named /etc/passwd:$ vi /etc/passwd
OR$ vim /etc/passwd
Search for a word named “vivek” in forward direction:
- Press ESC key
- Type /vivek
- Hit n to search forwards for the next occurrence of word named “vivek”. You can press N to search backwards.
How to search for a word in backwards in vim/vi
Let us open a file named demo.txt in the current directory:$ vi demo.txt
OR$ vim demo.txt
Search for a word named “bar” in backwards direction:
- Press ESC key
- Type ?bar
- Hit N to search backwards for the next occurrence of word named “bar”. You can press n to search forwards.
How to search for the current word
Say you have a file named data.txt as follows displayed using the cat command:$ cat data.txt
Sample outputs:
192.168.2.254 - default router 192.168.2.253 - wifi 192.168.2.252 - wifi bridge 192.168.2.254 - dns server 192.168.2.30 - backup server 192.168.2.254 - firewall 192.168.2.18 - vm server 192.168.2.203 - RHEL7 192.168.2.254 - dhcp server 192.168.2.200 - SUSE server 192.168.2.201 - FreeBSD nfs server
In normal mode, move the cursor to any word say 254. Press * to search forwards for the next occurrence of word 254, or press # to search backwards:
Search and open file from the CLI
The vi / vim text editor supports running any : command using the following syntax:vi +commandHere fileName
vim +LineNumber fileName
vi +/searchTermHere fileName
vi +/LineNumberHere fileName
vim +/LineNumberHere fileName
To open file and go to function called main(), enter:$ vim +/main filename-here
Next open file and go to line number 42, enter:$ vim +42 fileName
See “How to search for whole word in vim / vi
We can search for a whole word in VIM using the following syntax:
/<WORD-TO-SEARCH> # search for foo word /<FOO> # search for FreeBSD word /<FreeBSD> # Case sensitive vs insensitive search done by adding the C or c # /<FreeBSD> " Case sensitive /<FreeBSD>C " Case sensitive /<FreeBSD>c " Case insensitive
Case insensitive search in Vim
We can also do case insensitive search in Vim using the following two config options. For example, ignore case in search patterns, type::set ignorecase
Now search it to match Foo, foo, FOO and so on:/foo
Another option is to override the ‘ignorecase’ option if the search pattern contains upper case characters.:set smartcase "
Please note that the ‘ignorecase’ also applies to Getting help is easy for search commands. All you need to do is type the following commands::help /c
:help /C
:help 'ignorecase'
:help 'smartcase'
Conclusion
This page shows how to search using Vim or vi text editor for the word, including the use of the super star (*) to search for the current word. See vim help page for more info