2009-08-12 116 views

回答

60

作爲前一行程序:

:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch 

這使用Repeat組突出重複行。

其分解:

  • syn clear Repeat ::刪除以前發現重複
  • g/^\(.*\)\n\ze\%(.*\n\)*\1$/ ::對於在文件
    • 正則表達式
      • 在後面重復任意行^\(.*\)\n ::全系列
      • \ze ::比賽結束 - 驗證模式的休息,但不消耗匹配文本(正向前查找)
      • \%(.*\n\)* ::任意數量的實線
      • \1$ ::全線重複匹配實線
    • exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' ::添加匹配此到Repeat語法組
      • exe ::執行給定的字符串作爲Ex命令實線
      • getline('.') ::通過g//
      • escape(..., '".\^$*[]')匹配::轉義反斜槓給定字符的當前行的內容做出一個合法的正則表達式
      • syn match Repeat "^...$" ::給定的字符串添加到Repeat語法組
  • nohlsearch ::刪除從g//
做搜索突出

Justin的非正則表達式的方法是可能更快:

function! HighlightRepeats() range 
    let lineCounts = {} 
    let lineNum = a:firstline 
    while lineNum <= a:lastline 
    let lineText = getline(lineNum) 
    if lineText != "" 
     let lineCounts[lineText] = (has_key(lineCounts, lineText) ? lineCounts[lineText] : 0) + 1 
    endif 
    let lineNum = lineNum + 1 
    endwhile 
    exe 'syn clear Repeat' 
    for lineText in keys(lineCounts) 
    if lineCounts[lineText] >= 2 
     exe 'syn match Repeat "^' . escape(lineText, '".\^$*[]') . '$"' 
    endif 
    endfor 
endfunction 

command! -range=% HighlightRepeats <line1>,<line2>call HighlightRepeats() 
+1

非正則表達式方法閃電般快!很不錯的腳本,謝謝! – Hassek 2014-04-08 16:53:13

+0

這真的很棒! – pymarco 2014-08-01 16:09:24

+0

我不能得到這個工作。我把函數放在我的〜/ .vimrc中,但是當我運行「:調用HighlightRepeats()」時出現錯誤:處理函數時檢測到錯誤HighlightRepeats: line 10: E28:沒有這樣的高亮組名:Repeat – Daps0l 2014-11-15 12:46:24

3

通過列表運行一次,使地圖上每串多少次它發生。再次循環,並將*附加到地圖中具有多個值的字符串中。

+2

任何機會我們可以得到一些代碼? – technomalogical 2009-08-13 15:25:49

2

嘗試:

:%s:^\(.\+\)\n\1:\1*\r\1: 

希望這個作品。

更新:下次嘗試。

:%s:^\(.\+\)$\(\_.\+\)^\1$:\1\r\2\r\1*: 
+0

這隻會檢測相鄰的重複行,並且只會標記第一個副本,而不是第二個副本。 – rampion 2009-08-13 13:20:48

+0

你說得對。我再次嘗試。 – 2009-08-13 20:12:17

3

爲什麼不使用:

V* 

在正常模式。

它只是搜索當前行的所有比賽,從而突出它(如果啓用該設置,我認爲它是默認) 此外,你就可以使用

n 

要通過比賽

導航
+0

可視模式默認不支持*。這可能是你在.vimrc中的一個功能。事情是這樣的: XNO *: CAL VisualSearch()/ XNO#: CAL VisualSearch() 好玩! s:VisualSearch() let old = @「| norm!gvy let @/='\ V'.substitute(escape(@」,'\'),'\ n','\\ n','g ') let @「= old endf) – Michael 2009-08-17 06:11:52

+0

Arg,格式化搞砸了。下面是我的意思:http://pastebin.com/f2ee37c92 – Michael 2009-08-17 06:14:32

+0

是的,你是對的:) – Lonecat 2009-08-17 06:58:46

27

無上述工作對我的答案,所以這是我做的:

  1. 排序使用文件:sort
  2. 執行命令:g/^\(.*\)$\n\1$/p
+0

謝謝,我覺得這是更好的方法,通過這個我們可以找到重複的行以及定製到所需的長度 – harsha 2016-06-11 04:52:33

1
  1. :sort並將其保存在file1
  2. :sort u並將其保存在file2
  3. gvimdifftkdiff這兩個文件。