在 Vim 中导航一系列文件

发布于 2024-12-12 04:45:58 字数 829 浏览 0 评论 0原文

当修改代码时,我发现我经常需要浏览数十个文件才能更改最简单的内容。例如,假设我有一个函数 pretty_print,我将其更改为符合驼峰式大小写 prettyPrint。现在我想浏览文件 apple1.jsapple99.js,可能还有一些 orange.js 文件。 Vim 有没有快速的方法来做到这一点?

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

我知道我可以执行 :b,但是,尽管它支持名称完成/模式匹配,但我认为该模式不会被保留。

例如,如果我这样做

:b apple*.js

并点击选项卡,我会得到

:b apple1.js

,但是如果我重新访问该函数(通过按 : + upArrow 或 q:),那么如果我点击选项卡它不会转到

:b apple2.js

我想要的是指定诸如

:b apple*.js

编辑文件之类的内容,然后当我输入 :w 时,它会移动到下一个缓冲区。我宁愿留在 Vim 中,不想出来,输入 vim apple*.js,返回 Vim,然后使用 :x 命令。我意识到这是可行的,但我仍然需要所有其他文件,以防万一我想要,例如在标签之间跳转。

When modifying code I find I often have to go through dozens of files to change the simplest of things. For example lets say I have a function pretty_print and I change it to conform to camel case prettyPrint. Now I want to go through the files apple1.js to apple99.js, and possibly a few orange.js files in there. Is there a quick way to do this in Vim?

NOTE: this is not something I can automate, I actually need to go in and modify the code myself.

I know I can do :b <fileName>, but, although it supports name completion/pattern matching, I don't think the pattern is carried over.

For example, if I do

:b apple*.js

and I hit tab, I'll get

:b apple1.js

but if I revisit that function (either by pressing : + upArrow or q:) then if I hit tab it won't go to

:b apple2.js

What I want is to specify something like

:b apple*.js

edit the file, then when I type :w, it moves to the next buffer. I would prefer to stay in Vim, I don't want to come out, type vim apple*.js, go back into Vim and then use the :x command. I realize this works, but I still need all the other files in case I want to, for example jump between tags.

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

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

发布评论

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

评论(3

雨后咖啡店 2024-12-19 04:45:58

在这种情况下,最适合您的解决方案可能是使用
Vim 中集成了 grep 功能。以下命令执行搜索
对于与通配符 apple*.js 匹配的文件中的模式 \

, 并将模式出现的位置存储在快速修复列表中 允许轻松跳过所有比赛。

:vimgrep /\<pretty_print\>/ apple*.js

有关搜索的快速修复列表的更详细介绍
在文件中,请参阅 我对问题“加载获得的一组文件
通过 cmd-exec 进入 Vim 缓冲区
”。

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

:args apple*.js

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

Probably the most suitable solution for you in this case would be to use the
grep capabilities integrated in Vim. The following command performs a search
for the pattern \<pretty_print\> in files matching the wildcard apple*.js,
and stores locations of the pattern's occurrences in the quickfix list
allowing to easily jump through all the matches.

:vimgrep /\<pretty_print\>/ apple*.js

For more detailed introduction into the quickfix list with regard to searching
in files, see my answer to the question "Loading a set of files obtained
via cmd-exec into Vim buffers
".

If you would like to simply open a list of files matching a particular
wildcard, load that files' names into the argument list

:args apple*.js

and then navigate between them using :n and :N, as usual.

衣神在巴黎 2024-12-19 04:45:58

从这里开始:

: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

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

start with this:

: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

You better version your files and do a diff after such a change.

故人爱我别走 2024-12-19 04:45:58

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

Does the BufSel function from Wikia matches your needs?

If you would prefer to be able to select the buffer from the list of
partial matches
the following function can be used. It will jump to
the matching buffer if only one match is found, or if there are many
matches it will print a list of the matching buffers in the
command-line area, and allow you to select one of the matching buffers
by buffer number.

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