Vim - 拉入搜索寄存器

发布于 2024-08-22 03:07:31 字数 696 浏览 4 评论 0原文

有没有简单/快速的方法可以“猛拉”到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

冒着让一个很长的问题变得更长的风险,我想说的是,我并不总是想要“yank & 5个单词”。 search”——有时它是 17 个字符,有时它是到行尾,等等......所以硬编码的宏不能给我我想要的灵活性。

Is there any easy/quick way to "yank" into vim's "last search" register ("/)?

From the vim documentation, it appears that the answer is no, but that it can be assigned via a "let" command:

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.

Ideally what I'd like to do is something like:

"/5yw

which would yank the next 5 words under the cursor & put them in the last search buffer

Alternatively, if there is a way to search for the contents of a named register, that would work too. In other words, if I could do:

"A5yw

and then search for what is in register A, that would work too.

The closest I can come is to yank into a named register & then copy that register into the last search register, e.g.

"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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小巷里的女流氓 2024-08-29 03:07:31

/ 输入搜索字符串后,您可以使用 Ctrl-R 并键入代表您要使用的寄存器的字母。

例如。

  • 首先,"Ayw 将单词拉入寄存器 A
  • 然后,/^R A 将寄存器 A 的内容放入搜索字符串中。

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.
所有深爱都是秘密 2024-08-29 03:07:31

如果您没有使用任何寄存器来存储拉出的文本,vim 使用 0 寄存器。您可以通过在 / 之后输入 Ctrl-R 0 来搜索该内容。

一个更复杂的例子。假设您想在另一个缓冲区中搜索当前光标下引号内的文本:

  • 您可以使用 yi" (复制内部引号)
  • 转到您要搜索的缓冲区
  • 输入 /Ctrl-R 0

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
伴随着你 2024-08-29 03:07:31

我正在使用以下代码:

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

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
岁吢 2024-08-29 03:07:31

搜索选择:

如果您想首先拉取一行的一部分,然后使用“v”并移动光标,直到标记了您想要的内容,然后按 y 进行拉取,现在选择位于寄存器 0 中,

然后您可以输入 /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

糖粟与秋泊 2024-08-29 03:07:31

所以基本上是 # 和 * 命令的扩展版本,对吗?听起来您想定义一个自定义运算符(需要动作的命令)。我从来没有真正这样做过,但我确实找到了一个 插件看起来这可能会更容易做到这一点。提供了一些示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文