1

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

sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg  dfgfdg

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

我理想地想要做的是直观地选择代码块,然后按搜索来查找所有出现的地方。

4

4 回答 4

2

也许你应该看看: 搜索视觉选择的文本

我从这里拿走了

于 2009-08-31T13:18:22.577 回答
2

试试这个。将此脚本包含在您的运行时路径中的某处(请参阅 参考资料: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
于 2009-08-31T13:20:58.033 回答
1

正在搜索的文本存储在/寄存器中。您不能直接将其拉出或删除到此寄存器中,但您可以使用“让”分配给它。

试试这个:

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

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

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

于 2009-08-31T13:28:07.900 回答
1

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

:set hlsearch
*

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

于 2009-09-04T16:25:16.010 回答