输入搜索结果

是否有任何简单/快捷的方法可以“猛拉”到 vim 的“最后一次搜寻”登记册(”/) ?

从 vim 文档来看,答案似乎是否定的,但是可以通过“ let”命令分配:

It is writable with ":let", you can change it to have 'hlsearch' highlight
other matches without actually searching.  You can't yank or delete into this
register.

理想情况下,我想做的是:

"/5yw

它会将接下来的5个单词拖到光标下,并将它们放在最后一个搜索缓冲区中

或者,如果有一种方法可以搜索指定寄存器的内容,那也是可行的。换句话说,如果我能做到:

"A5yw

然后搜索注册表 A 中的内容,这也可以。

最接近的方法是猛拉一个指定的登记册,然后将该登记册复制到最后一个搜寻登记册,例如。

"A5yw
:let @/=@A

At the risk of making a long question longer, I want to state that it's not always 5 words I'd like to "yank & search" -- sometimes it's 17 characters, sometimes it's to the end of the line, etc... so a hard-coded macro doesn't give me the flexibility I'd want.

29791 次浏览

So basically an extended version of the # and * commands, right? It sounds like you want to define a custom operator (a command that expects a motion). I've never actually done this, but I did find a plugin which looks like it might make it easier to do so. There are some examples provided.

After pressing / to enter a search string, you can then use Ctrl-R and then type the letter representing the register that you want to use.

eg.

  • First, "Ayw to yank a word into register A
  • Then, / ^R A to put the contents of register A into the search string.

I'm using following code for that:

vnoremap <silent>* <ESC>:call VisualSearch('/')<CR>/<CR>
vnoremap <silent># <ESC>:call VisualSearch('?')<CR>?<CR>


function! VisualSearch(dirrection)
let l:register=@@
normal! gvy
let l:search=escape(@@, '$.*/\[]')
if a:dirrection=='/'
execute 'normal! /'.l:search
else
execute 'normal! ?'.l:search
endif
let @/=l:search
normal! gV
let @@=l:register
endfunction

If you did not use any register to store the yanked text vim uses 0 register. You can search for that by typing Ctrl-R 0 after /.

A more complicated example. Say that you want to search in another buffer for the text inside quotes which is under the cursor right now:

  • You can do that with yi" (yank inner quote)
  • Go to the buffer where you want to search
  • Type /Ctrl-R 0

Searching for a selection:

if you want to first yank a section of a line, then use "v" and move with cursors until you have marked what you want, then press y for yank and now the selection is in register 0

you can then type /Ctrl-R 0