Skip to content

Instantly share code, notes, and snippets.

@ephbaum
Last active June 23, 2023 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ephbaum/c480fd0ba65b097dee36734a20a8de43 to your computer and use it in GitHub Desktop.
Save ephbaum/c480fd0ba65b097dee36734a20a8de43 to your computer and use it in GitHub Desktop.
A vim command that will take a list in a buffer and find & remove 1 (or more) duplicate lines (instead of using `:sort u`
:g/^\(.*\)\n\1$/d
  • :g: The global (g) command in Vim, performs a certain action on lines that match a given pattern.
  • /^: This symbol denotes the start of a line.
  • \( and \): These parentheses are used for grouping in regex. Everything between \( and \) is treated as a single group. This group can be referenced later.
  • .*: The dot (.) stands for any character except a newline. The asterisk (*) is a quantifier, meaning "zero or more of the preceding element". So, .* matches any string, including an empty string.
  • \n: This represents a newline character.
  • \1: This is a backreference to the first (and only) group that we defined earlier. In this case, it refers to the exact string that was matched inside the \( and \).
  • $: This symbol denotes the end of a line.
  • /d: The d command deletes the lines that match the given pattern.

In summary, the regular expression :g/^\(.*\)\n\1$/d performs the following operation:

  • It looks for lines in a text that are immediately followed by an identical line.
  • If such pairs of identical lines are found, both lines are deleted.

OR

:%sort u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment