Skip to content
Log In Sign Up info@muvcloud.com

Search Forward and Backward

Press Esc to be in normal mode, type a slash followed by the text, and press Enter:

/server_name
?listen

/ searches forward from the cursor and ? searches backward. The cursor jumps to the first match. If nothing matches, Vim reports E486: Pattern not found and the cursor stays where it was. To cancel a search you have started typing, press Esc instead of Enter. Searching never changes the text.

Jump Between Matches

KeyWhat it does
nNext match, same direction.
NNext match, other direction.
/ then EnterRepeats the last search forward.
/ then UpRecalls earlier searches.
Ctrl+oBack to where you were.

At the end of the file the search continues from the top, and Vim says so in the message line with "search hit BOTTOM, continuing at TOP". To stop at the end instead, type :set nowrapscan.

Search for the Word Under the Cursor

In normal mode, put the cursor on a word and press * to find its next occurrence, or # to find the previous one. Both match the whole word only: on root they skip chroot. g* and g# also match the word inside longer words. After either, n and N continue the search.

Case-Sensitive or Not

Searches are case-sensitive by default. Change that with these commands in normal mode:

:set ignorecase
:set smartcase
/error\c
/Error\C
  • ignorecase: every search ignores case.
  • smartcase: with ignorecase on, a typed pattern with a capital letter becomes case-sensitive again. /error then finds Error and ERROR, while /Error finds only Error. It does not apply to * and #.
  • \c and \C: anywhere in a pattern, they make that one search ignore case or respect it.

Highlight Matches and Clear the Highlight

:set hlsearch
:set incsearch
:noh

hlsearch highlights every match, and incsearch shows the first match while you type. :noh, short for :nohlsearch, removes the highlight until the next search without turning the option off. Put the set lines in ~/.vimrc to keep them. Neovim has both options on by default.

Characters with a Special Meaning

A search pattern is a regular expression. A dot matches any character, so /127.0.0.1 also finds other text. Put a backslash before characters you mean literally:

/127\.0\.0\.1
/\/var\/www
/^Port
/;$
/\<root\>

The lines find an exact IP address, a path (the slash is escaped because it ends the pattern), a line that starts with Port, a line that ends with a semicolon, and the whole word root.

Search and Replace with :s

Replacing changes the text in Vim only; the file on disk changes when you save. Press Esc, then type:

:s/old/new/
:s/old/new/g
:%s/old/new/g
:%s/old/new/gc
:10,20s/old/new/g
  • Without a range: only the current line; g replaces every match on it, not only the first.
  • %: the whole file. 10,20 limits it to lines 10 to 20.
  • c: asks at each match; answer y, n, a for all, or q to stop.

For paths, use another delimiter so you need no backslashes: :%s#/var/www/old#/var/www/new#g. An empty pattern reuses the last search: after /listen 80, the command :%s//listen 8080/g replaces it. One u undoes the whole replacement; see how to undo and redo in Vim.

Searching on a Server

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations