이원준
이원준

Reputation: 3

Removing contiguous duplicate lines in VIM exception condition without sorting

I would like to perform the remove contiguous duplicate lines step with exception condition and without sorting in VIM

Example below:

Before Regex

a
b
c
d
00
f
b
00
c
e
00

After Run Regex

a
b
c
d
00
f
00
e
00

I want to delete duplicate lines without delete "00" pattern.

Upvotes: 0

Views: 113

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172648

My PatternsOnText plugin provides (among others) this command:

:%DeleteDuplicateLinesIgnoring /00/

Upvotes: 0

Kent
Kent

Reputation: 195169

Vim is very powerful editor, however for this problem, I would turn to external utility for an easy solution.

If you have awk available(which is default installed on most linux distributions), you can do this in your vim:

:%!awk '/^00$/||\!a[$0]++'

Upvotes: 2

Luc Hermitte
Luc Hermitte

Reputation: 32966

With the following, it should work:

function! s:HandleLine()
  let line = getline('.')
  if has_key(s:seen, line)
    delete
  else
    let s:seen[line] = 1
  endif
endfunction


command! -range=% -nargs=1 UnsortUniq let s:seen={}<bar><line1>,<line2>v/<args>/call s:HandleLine()

Then execute :%UnsorUniq ^00$

Upvotes: 2

Related Questions