How do I remove duplicate consecutive lines using a substitute command?
:%s/^\(.\+\)\n\1$/\1/
This substitute command detects pairs of identical adjacent lines and collapses them into one, using a back-reference to match the repeated content.
:%s/^\(.\+\)\n\1$/\1/
This substitute command detects pairs of identical adjacent lines and collapses them into one, using a back-reference to match the repeated content.
cgn...{text}<Esc>.
The cgn + .
[[:alpha:]]
Vim's regex engine supports POSIX character classes inside bracket expressions, giving you locale-aware, readable alternatives to manual character ranges like [
\_.{-}
The \ modifier in Vim extends any character class or atom to also match newlines.
matchadd('Group', 'pattern', priority)
matchadd() accepts an optional third argument: a priority integer that controls which match wins when two patterns cover the same text.
:echo synIDattr(synID(line('.'),col('.'),1),'name')
When customizing a colorscheme or debugging unexpected colors, you need to know exactly which highlight group is coloring the text under your cursor.
:let @a = @/
Vim stores the last search pattern in the special / register (@/).
:g/\(.\+\)\n\1/d
The :g command with a backreference pattern can detect and delete consecutive duplicate lines in one pass.
/pattern/e+2
Vim's search command supports an offset suffix that controls where the cursor lands after a match.
:[range]sort /regex/
The :[range]sort /regex/ command sorts lines while skipping the portion of each line that matches the regex.
\%'m
Vim's \%'m regex atom matches the exact position of mark m in a search pattern.
:s/\v(\w+)\s+(\w+)/\2 \1/
By using capture groups in a substitute command with very magic mode (\v), you can swap two adjacent words in a single operation.
/pattern/;+3
In Vim's range notation, a semicolon (;) between two addresses makes the second address relative to the line the first address matched, not to the current curso
:s/pattern/~/
In a Vim substitution, using ~ as the replacement string repeats the replacement text from the most recent :s command.
\(pattern\)\@=
Vim's regex engine supports zero-width positive lookahead via the \@= operator.
s/pattern/\r/
In Vim substitutions, \r in the replacement string inserts a line break, creating a new line.
\_.
In Vim's regular expressions, .
[* and ]*
The [ and ] motions (also written as [/ and ]/) let you jump directly to the opening / or closing / of a C-style block comment.
:%s/\d\+/\=printf('%03d', submatch(0))/g
Combining Vim's \= expression substitution with the printf() function lets you apply arbitrary formatting to matched text.
matchadd()
The matchadd() function adds a persistent highlight for a pattern in the current window without touching your search register or interfering with n/N navigation