【问题标题】:Identifying Identical Blocks of Code识别相同的代码块
【发布时间】:2010-11-24 07:56:54
【问题描述】:

假设我在一个文件中有多个以下代码块(空格无关紧要):

sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg  dfgfdg

您如何找到/突出显示所有事件?

我最理想的做法是直观地选择代码块,然后按搜索查找所有匹配项。

【问题讨论】:

    标签: vim full-text-search text-editor vi


    【解决方案1】:

    快速而肮脏的部分解决方案:

    :set hlsearch
    *
    

    hlsearch 选项(在某些 vim 配置中默认打开,但我总是将其关闭)使 vim 突出显示当前搜索的所有找到的实例。在正常模式下按* 搜索光标下的单词。所以这将突出显示光标下单词的所有实例。

    【讨论】:

      【解决方案2】:

      正在搜索的文本存储在/ 寄存器中。您不能直接将其拉入或删除到此寄存器中,但您可以使用 `let' 对其进行分配。

      试试这个:

      • 使用可视模式突出显示您要搜索的代码
      • 键入"ay 将突出显示的选择拉入寄存器a
      • 键入:let @/ = @a 将寄存器a 复制到搜索寄存器/

      此时,与您的选择匹配的所有代码都将突出显示,您可以使用 n/N 浏览出现的事件,就像常规搜索一样。

      当然,您可以使用任何临时寄存器来代替a。并且映射这个命令序列以方便使用应该不会太难。

      【讨论】:

        【解决方案3】:

        试试这个。将此脚本包含在您的运行时路径中的某处(请参阅:help runtimepath)。一个简单的选择是将它放在你的 vimrc 中。直观地选择您要搜索的内容,然后按,/(逗号键,然后是正斜杠键)。

        " Search for other instances of the current visual range
        
        " This works by:
        " <ESC>                Cancel the visual range (it's location is remembered)
        " /                    Start the search
        " <C-R>=               Insert the result of an expression on
        "                      the search line (see :help c_CTRL-R_= )
        " GetVisualRange()<CR> Call the function created below
        " <CR>                 Run the search
        vmap ,/ <ESC>/<C-R>=GetVisualRange()<CR><CR>
        
        " Create the function that extracts the contents of the visual range
        function! GetVisualRange()
            " Get the start and end positions of the current range
            let StartPosition = getpos("'<")
            let EndPosition = getpos("'>")
        
            " Prefix the range with \V to disable "magic"
            " See :help \V
            let VisualRange = '\V'
        
            " If the start and end of the range are on the same line
            if StartPosition[1] == EndPosition[1]
                " Just extract the relevant part of the line
                let VisualRange .= getline(StartPosition[1])[StartPosition[2]-1:EndPosition[2]-1]
            else
                " Otherwise, get the end of the first line
                let VisualRange .= getline(StartPosition[1])[StartPosition[2]-1:]
                " Then the all of the intermediate lines
                for LineNum in range(StartPosition[1]+1, EndPosition[1]-1)
                    let VisualRange .= '\n' . getline(LineNum)
                endfor
                " Then the start of the last line
                let VisualRange .= '\n' . getline(EndPosition[1])[:EndPosition[2]-1]
            endif
            " Replace legitimate backslashes with double backslashes to prevent
            " a literal \t being interpreted as a tab
            let VisualRange = substitute(VisualRange, '\\[nV]\@!', '\\\\', "g")
        
            " Return the result
            return VisualRange
        
        endfunction
        

        【讨论】:

          【解决方案4】:

          也许你应该看看: Search for visually selected text

          我是从here那里拿的

          【讨论】:

          • 啊,我不知道那个。本来可以让我免于现在写自己的。呃,好吧。复制视觉选择然后使用 Ctrl-R-" 访问它很有意义。我正在尝试(我希望我成功)编写一个不依赖于映射的函数,但看到它很有趣另一种方法。
          猜你喜欢
          • 1970-01-01
          • 2015-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          • 2019-04-07
          • 2010-09-24
          相关资源
          最近更新 更多