:[range]s/{pattern}/{replace}/[flags] [count]no specifier: current line..: current line.$: last line.line1,line2: range from line1 to line2, both inclusive, first line is indexed 1 not 0.%: whole file.1,$: whole file..,$: from current line to the end.'<,'>: visual selection (start / end).line+N: N lines after line.line-N: N lines before line.\< and \>: word boundaries.^word: start with word.word$: end with word..: match exactly one character except newline..*: greedy match..\{-}: non-greedy match.\_.: match exactly one character including newline.\: escape special character.\n: match newline.\(<content>\): match a group.\r: newline.\t: tab.g: replace all matches per line.c: confirm each substitution.i: case-insensitive mode.I: case-sensitive mode.n: no change, only highlight.e: no error if nothing found.| Symbol | Meaning | Example |
|---|---|---|
% |
The current file name | :! echo % or :echo expand('%') gets main.c |
%:p |
Full absolute path of the current file | :! echo %:p or :echo expand('%:p') gets /home/user/proj/src/main.c |
%:p:h |
Directory (head) of the current file | :! echo %:p:h or :echo expand('%:p:h') gets /home/user/proj/src |
%:t |
Tail (basename only) | :! echo %:t or :echo expand('%:t') gets main.c |
%:r |
Root (filename without extension) | :! echo %:r or :echo expand('%:r') gets main |
%:e |
Extension name only | :! echo %:e or :echo expand('%:e') gets c |
:[range]!sed 's/{pattern}/{replace}/{flags}'.: current line only.%: entire file (most common).'<,'>: visual selection.1,10: lines 1 through 10..,$: current line to end of file.:s:| Task | Vim Command | Equivalent Native Vim |
|---|---|---|
| Simple substitution | :%!sed 's/foo/bar/g' |
:%s/foo/bar/g (faster) |
| Delete empty lines | :%!sed '/^$/d' |
:g/^$/d (better) |
| Delete lines with pattern | :%!sed '/pattern/d' |
:g/pattern/d |
| Print specific lines | :%!sed -n '5,10p' |
:5,10 (view only) |
| Insert line before match | :%!sed '/pattern/i\New line' |
:g/pattern/norm! ONew line |
| Append line after match | :%!sed '/pattern/a\New line' |
:g/pattern/norm! oNew line |
| Multiple operations | :%!sed 's/a/A/g; s/b/B/g' |
:%s/a/A/g | %s/b/B/g |
| Case conversion (uppercase) | :%!sed 's/.*/\U&/' |
:% norm! gUU or :%s/.*/\U&/ |
\+: one or more (in basic sed, or use sed -E for +).\?: zero or one (in basic sed, or use sed -E for ?).\|: alternation (in basic sed, or use sed -E for |).\( \): capture groups (always escaped in basic sed).&: reference the entire match in replacement.\1, \2: backreferences (same as Vim).:%!sed -E 's/(foo)(bar)/\2\1/'| Description | Command |
|---|---|
| Filter current line through sed | :.!sed 's/old/new/g' |
| Filter visual selection | :'<,'>!sed 's/old/new/g' |
| Remove trailing whitespace | :%!sed 's/[[:space:]]*$//' |
| Number all lines | :%!sed = | sed 'N;s/\n/\t/' |
| Double-space document | :%!sed G |
| Delete HTML tags | :%!sed 's/<[^>]*>//g' |
| Convert DOS to Unix line endings | :%!sed 's/\r$//' |
| Swap first two fields (CSV) | :%!sed -E 's/([^,]*),([^,]*)/\2,\1/' |
| Use sed when... | Use Vim's :s when... |
|---|---|
| You need a specific sed script you already have | Doing interactive editing (faster, better undo) |
| Using complex sed-specific features | Performance matters (no process spawning) |
| Piping through multiple filters | Need Vim registers or expression evaluation |
| Combining with other Unix tools | Working with visual selections frequently |
| Testing sed script for shell use | General text editing (native Vim is superior) |
:%!sed 's/foo/bar/' | grep bar: substitute then filter.:%!sort | sed 's/^/- /': sort lines, then add bullet.:%!sed 's/</\</g; s/>/\>/g': HTML escape.