I think the following command should work :
We use the substitution command on the whole file to change
pattern can be broken down like this :
$ match respectively a beginning of line and an end of line.
\) are used to enclose
subpattern1 so that we can refer to it later by the special number
They are also used to enclose
subpattern2 so that we can repeat it 1 or more times with the quantifier
. is a metacharacter matching any character except new line and
* is a quantifier that matches the last character 0, 1 or more times.
.* matches any text containing no new line.
\n matches a new line and
\1 matches the same text that was matched inside the first
\) which here is
pattern can be read like this :
a beginning of line (
^) followed by any text containing no new line (
.*) followed by a new line (
\n) then the same text (
\1), the latter two being repeated one or more times (
\+), and finally an end of line (
pattern is matched (a block of identical lines), the substitution command replaces it with
string which here is
\1 (the first line of the block).
If you want to see which blocks of lines will be affected without changing anything in your file, you can enable the
hlsearch option and add the
n substitution flag at the end of the command :
For more granular control, you can also ask for a confirmation before changing each block of lines by adding the
c substitution flag instead :
For more information on the substitution command read
for the substitution flags
for the various metacharacters and quantifiers read
and for regular expressions in vim read this.
Edit: Wildcard fixed a problem in the command by adding a
$ at the end of
Also BloodGain has a shorter and more readable version of the same command.