2009-08-31 75 views
1

的相同的块说我有下面的代码的多个文件中的块(空格是irrelavent):验证码

sdgfsdg dfg 
dfgdfgf ddfg 
dfgdfgdfg dfgfdg 

你如何找到/突出显示所有出现?

我想要做的是从视觉上选择代码块,然后按搜索以查找所有事件。

回答

1

文本正在搜索被存储在/寄存器。你不能直接插入或者删除这个寄存器,但是你可以用`let'来分配它。

试试这个:

  • 使用可视化模式,以突出显示要查找
  • "ay类型猛拉,突出选择到寄存器a
  • :let @/ = @a类型复制登记a到搜索代码寄存器/

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

当然,您可以使用任何临时寄存器而不是a。将这个命令序列映射为便于使用应该不会太困难。

2

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

我从here

+0

啊,我不知道那个。刚才我已经救了我自己写了自己的东西。呃,好吧。复制视觉选择很有意义,然后使用Ctrl-R-“来访问它,我试图(并且我希望我成功了)编写一个不依赖于映射的函数,但有趣的是看到这样做的另一种方式。 – DrAl 2009-08-31 13:23:20

+0

+1这是一个很好的努力。 – LB40 2009-08-31 13:35:32

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 
1

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

:set hlsearch 
* 

hlsearch选项(默认情况下在一些VIM CONFIGS,但我总是把它关掉),使VIM突出显示当前搜索找到的所有实例。在正常模式下按*将搜索光标下的单词。所以这会突出显示光标下的单词的所有实例。