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.
795 results for "g* g#"
:%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.
:cdo s/foo/bar/ge | update
When quickfix already contains exactly the lines you want to touch, :cdo is the safest way to batch-edit with tight scope.
command-line #command-line #quickfix #substitution #refactoring
:e +{line} {file}
The :edit command accepts a +{cmd} prefix that executes an Ex command immediately after the file is loaded.
command-line #buffers #ex-commands #navigation #command-line
:let @/='\V'.escape(@", '\\')
When you yank text that contains regex characters like .
/\cpattern
How it works By default, Vim searches are case-sensitive: /Hello will not match hello or HELLO.
:set nrformats+=hex,bin
By default, Vim's and only increment and decrement decimal numbers.
<C-a> (insert mode)
While in insert mode, pressing re-inserts the exact text you typed during your previous insert session.
= (in visual mode)
After making a visual selection, pressing = applies Vim's auto-indent to every selected line at once.
<C-v>{motion}~
Visual block mode () selects a rectangular column region, and pressing ~ at the end toggles the case of every character in that exact column range across all se
:set diffopt+=algorithm:patience
Vim's default diff algorithm (Myers) can produce noisy diffs when working with code — it sometimes matches unrelated lines containing common tokens like {, },
cgn
The cgn command combines the change operator with the gn motion to change the next occurrence of your last search pattern.
<C-r><C-w>
When typing a command on the Vim command line, pressing inserts the word currently under the cursor.
<C-w>N
When using Vim's built-in :terminal, the buffer is in Terminal-Job mode by default, meaning all keystrokes go to the running shell.
!{motion}sort
The ! operator in Vim filters a motion's text through an external shell command, replacing it with the output.
:m +1
The :move command (abbreviated :m) relocates lines to a new position in the buffer without touching any registers.
fnameescape(filename)
When building Vim Ex commands dynamically, filenames with spaces, , %, #, [, or other special characters will be misinterpreted — the space looks like an argu
:match {group} /pattern/
:match, :2match, and :3match give you three independent highlight slots that overlay patterns on the buffer using any highlight group — without touching the s
:execute 'vimgrep /' . @/ . '/gj **/*'
If you already refined a search interactively with / or ?, retyping that pattern for project-wide grep is repetitive and error-prone.
$
The $ command moves the cursor to the last character of the current line.
p (in visual mode)
In visual mode, pressing p replaces the selected text with the contents of the default register.