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
| Key | What it does |
|---|---|
n | Next match, same direction. |
N | Next match, other direction. |
/ then Enter | Repeats the last search forward. |
/ then Up | Recalls earlier searches. |
Ctrl+o | Back 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.
/errorthen finds Error and ERROR, while/Errorfinds only Error. It does not apply to*and#. \cand\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
:nohhlsearch 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;
greplaces every match on it, not only the first. %: the whole file.10,20limits it to lines 10 to 20.c: asks at each match; answery,n,afor all, orqto 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
- Open at the first match:
vim +/client_starts at the line you need.max /etc/nginx/nginx.conf - Find the right file first:
grep -rn client_shows file names and line numbers; more commands are in the Linux commands cheat sheet.max /etc/nginx - Go to a line number from an error: see how to go to the end of a file in Vim.
- After editing: save as described in how to save and quit in Vim, then test the configuration with
sudo nginx -t.
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.
More Guides
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.

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