的相同的塊說我有下面的代碼的多個文件中的塊(空格是irrelavent):驗證碼
sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg dfgfdg
你如何找到/突出顯示所有出現?
我想要做的是從視覺上選擇代碼塊,然後按搜索以查找所有事件。
的相同的塊說我有下面的代碼的多個文件中的塊(空格是irrelavent):驗證碼
sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg dfgfdg
你如何找到/突出顯示所有出現?
我想要做的是從視覺上選擇代碼塊,然後按搜索以查找所有事件。
文本正在搜索被存儲在/
寄存器。你不能直接插入或者刪除這個寄存器,但是你可以用`let'來分配它。
試試這個:
"ay
類型猛拉,突出選擇到寄存器a
:let @/ = @a
類型複製登記a
到搜索代碼寄存器/
此時,所有代碼匹配您的選擇將突出顯示,並且您可以像使用常規搜索一樣使用n/N瀏覽事件。
當然,您可以使用任何臨時寄存器而不是a
。將這個命令序列映射爲便於使用應該不會太困難。
也許你應該看看: Search for visually selected text
我從here
採取它試試這個。將此腳本包含在運行時路徑的某處(請參閱: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
快速和骯髒的部分解決方案:
:set hlsearch
*
的hlsearch
選項(默認情況下在一些VIM CONFIGS,但我總是把它關掉),使VIM突出顯示當前搜索找到的所有實例。在正常模式下按*
將搜索光標下的單詞。所以這會突出顯示光標下的單詞的所有實例。
啊,我不知道那個。剛纔我已經救了我自己寫了自己的東西。呃,好吧。複製視覺選擇很有意義,然後使用Ctrl-R-「來訪問它,我試圖(並且我希望我成功了)編寫一個不依賴於映射的函數,但有趣的是看到這樣做的另一種方式。 – DrAl 2009-08-31 13:23:20
+1這是一個很好的努力。 – LB40 2009-08-31 13:35:32