在 Vim 中浏览一系列文件

3

当我修改代码时,经常需要浏览几十个文件才能完成最简单的事情。例如,假设我有一个函数pretty_print,我将其改为符合驼峰命名法的prettyPrint。现在我想浏览apple1.jsapple99.js中的文件,并可能在其中包含一些orange.js文件。在Vim中有快速的方法吗?

注意:这不是我可以自动化的东西,我实际上需要进去并修改代码。

我知道我可以使用:b <fileName>,但是,尽管它支持名称完成/模式匹配,但我认为该模式没有传递下去。

例如,如果我执行

:b apple*.js

当我按Tab键时,会出现:

:b apple1.js

但是,如果我重新访问该函数(通过按: + 上箭头或q:),那么如果我按tab键,它就不会跳转到

:b apple2.js

我想要的是指定类似于以下内容:

:b apple*.js

编辑文件后,当我输入:w时,它会移动到下一个缓冲区。我更喜欢留在Vim中,不想退出,输入vim apple*.js,再回到Vim并使用:x命令。我知道这样做可以达到目的,但如果我想跳转标签,我仍然需要所有其他文件。
3个回答

1

Wikia的BufSel函数是否符合您的需求?

如果您希望能够从部分匹配列表中选择缓冲区,则可以使用以下函数。如果只找到一个匹配项,则跳转到匹配的缓冲区;如果有多个匹配项,则在命令行区域打印匹配缓冲区的列表,并允许您通过缓冲区编号选择其中之一。

function! BufSel(pattern)
  let bufcount = bufnr("$")
  let currbufnr = 1
  let nummatches = 0
  let firstmatchingbufnr = 0
  while currbufnr <= bufcount
    if(bufexists(currbufnr))
      let currbufname = bufname(currbufnr)
      if(match(currbufname, a:pattern) > -1)
        echo currbufnr . ": ". bufname(currbufnr)
        let nummatches += 1
        let firstmatchingbufnr = currbufnr
      endif
    endif
    let currbufnr = currbufnr + 1
  endwhile
  if(nummatches == 1)
    execute ":buffer ". firstmatchingbufnr
  elseif(nummatches > 1)
    let desiredbufnr = input("Enter buffer number: ")
    if(strlen(desiredbufnr) != 0)
      execute ":buffer ". desiredbufnr
    endif
  else
    echo "No matching buffers"
  endif
endfunction

"Bind the BufSel() function to a user-command
command! -nargs=1 Bs :call BufSel("<args>")

我最初确实安装了BufSel并使用它,但后来放弃了,我想知道它是否能够满足我的需求。无论如何,它与:b相比并没有多大改进,而且关闭vim并仅重新启动我想要编辑的文件会更容易些。 - puk

1
在这种情况下,您可能最合适的解决方案是使用 Vim 集成的 grep 功能。以下命令在匹配通配符 apple*.js 的文件中搜索模式 \<pretty_print\>,并将模式出现的位置存储在快速修复列表中,使您可以轻松跳转到所有匹配项。
:vimgrep /\<pretty_print\>/ apple*.js

如果您想了解有关在文件中搜索的快速修复列表的更详细介绍,请参阅我对问题“将通过cmd-exec获取的一组文件加载到Vim缓冲区中”的答案

如果您只想打开与特定通配符匹配的文件列表,请将这些文件名加载到参数列表中。

:args apple*.js

然后像往常一样使用:n:N在它们之间导航。


1

从这里开始:

:set hidden "required because `argdo` won't load next argument into current window
            "if there is a modified buffer displayed inside this window
:args apple*.js
:argdo %s/\<pretty_print\>/prettyPrint/g
:rewind " if you want to proofread all files then use :next and :prev
:wa

在进行此类更改后,最好对您的文件进行版本控制并进行差异比较。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接