What is the basic replace command?
:%s/listen 80/listen 8080/g
: enters command mode, % means every line in the file, s is substitute, then the search pattern, the replacement and flags. g replaces every match on each line rather than just the first. Vim is available on every image we install as vim or the smaller vi; on minimal Debian installs, apt install vim adds the full version with syntax highlighting.
Which flags change the behavior?
| Flag | Effect |
|---|---|
g | All matches on each line |
c | Confirm each replacement |
i / I | Ignore case / match case |
n | Count matches without replacing |
e | No error when nothing matches |
With c, Vim stops at each match and asks: y to replace, n to skip, a for all remaining, q to quit, l to replace this one and stop. It is the safe choice for config files where a pattern might match more than you expect. :%s/worker_processes//gn tells you how many matches exist before changing anything.
How do I limit the replacement to some lines?
:10,40s/127.0.0.1/0.0.0.0/g " lines 10 to 40
:.,$s/debug/info/g " current line to the end
:.,+5s/^/# / " comment out this line and the next five
:/server {/,/^}/s/80/8080/g " from a pattern to another pattern
For a visual selection, press V, move to select lines, then type :. Vim fills in :'<,'>, the selection's range, and you add s/old/new/g.
How do I replace a word but not longer words containing it?
Wrap the word in \< and \>:
:%s/\<user\>/account/g
This changes user but leaves username and users alone. A shortcut: put the cursor on a word and press * to search for it as a whole word; then :%s//account/g reuses the last search pattern, because an empty pattern means "the previous one".
How do regex groups work in Vim?
Vim's default regex needs backslashes before groups and some operators. The "very magic" prefix \v makes patterns look like the extended regex used by grep -E and most languages:
:%s/\v(\d+)\.(\d+)\.(\d+)\.(\d+)/\4.\3.\2.\1/g " reverse IPv4 octets :%s/\vmax_children \= \d+/max_children = 32/ " set a PHP-FPM value
In the replacement, \1 to \9 insert captured groups and & inserts the whole match. \u and \U uppercase the next character or the rest, which helps with renames.
What if the text contains slashes?
Use another delimiter. Any single-byte character works, which keeps paths readable:
:%s#/var/www/old#/srv/site#g
How do I act on every line that matches?
The global command :g runs another command on each matching line:
:g/^\s*#/d " delete comment lines :g/error/s/warn/notice/g " replace only on lines containing error :v/\S/d " delete blank lines (v inverts the match)
Can I replace across several files?
Yes, with the quickfix list:
:vimgrep /old_api_url/gj /srv/app/config/**/*.yml :cdo s/old_api_url/new_api_url/g | update
:vimgrep collects matches into the quickfix list; :cdo runs the substitute on each entry and update saves changed files. For scripted, non-interactive edits on many servers, sed is the better tool; see The sed command.
How do I undo a bad replacement?
Press u in normal mode; one substitute command is a single undo step, however many lines it changed. Ctrl+R redoes. Before editing important configs, copying the file with cp nginx.conf nginx.conf.orig remains the simplest safety net, and testing the result with the service's own check, such as nginx -t, catches mistakes before a reload.
How do I search before replacing?
Type /pattern and press Enter to jump to the first match, then n and N to move forward and back. :set hlsearch highlights every match, so you can see what a substitute will touch. Once the search looks right, :%s//new/g reuses it without retyping.
Keep Reading
The sed command
Edit text streams and files with sed: substitutions, in-place edits with backups, line addresses, extended regex, multiple commands and GNU versus BSD.
OpenThe grep command
grep from basics to power use: case, whole words, line numbers, context, recursion, regex flavors, only-matching output and speed tips.
OpenFind a file on Linux
The fastest way to locate a file by name, by package, by command name or by content, from plocate and fd to find and dpkg -S.
OpenLinux file permissions
Read ls -l output, understand owner, group and other bits, umask defaults, ACLs and why root and SELinux sit on top of it all.
OpenFAQ
Quick Answers
Type :%s/old/new/g and press Enter.
Add the c flag, as in :%s/old/new/gc, and answer y or n at each match.
Wrap the word in \< and \>, for example :%s/\/account/g.
Use another delimiter, such as :%s#/old/path#/new/path#g.
Press u in normal mode; the whole substitute is undone in one step.