我已经开始使用Vim来开发Perl脚本,并开始发现它非常强大。

我喜欢的一件事是能够同时打开多个文件:

vi main.pl maintenance.pl

然后在他们之间跳来跳去

:n
:prev

然后查看哪个文件是打开的:

:args

要添加一个文件,我可以说:

:n test.pl

我希望它会被添加到我的文件列表中,但相反,它会清除我当前的文件列表,当我输入:args时,我只有test.pl打开。

那么我如何在我的args列表中添加和删除文件?


当前回答

这个帖子中的大多数答案都是使用普通的vim命令,这当然很好,但我认为我将使用我认为特别有用的插件和函数的组合来提供一个广泛的答案(至少其中一些技巧来自Gary Bernhardt的文件导航技巧):

To toggle between the last two file just press <leader> twice. I recommend assigning <leader> to the spacebar: nnoremap <leader><leader> <c-^> For quickly moving around a project the answer is a fuzzy matching solution such as CtrlP. I bind it to <leader>a for quick access. In the case I want to see a visual representation of the currently open buffers I use the BufExplorer plugin. Simple but effective. If I want to browse around the file system I would use the command line or an external utility (Quicklsilver, Afred etc.) but to look at the current project structure NERD Tree is a classic. Do not use this though in the place of 2 as your main file finding method. It will really slow you down. I use the binding <leader>ff.

这些应该足够查找和打开文件了。从那里当然使用水平和垂直分割。关于分割,我发现这些函数特别有用:

当没有足够的空间时,在较小的区域打开新的裂缝,并在导航时扩展它们。参考这里的评论,这些具体做什么: 设置winwidth = 84 设置winheight = 5 设置winminheight = 5 设置winheight = 999 nnoremap <C-w>v:111vs<CR> nnoremap <C-w>s:rightbelow split<CR> 设置splitright 轻松地从一个分裂到另一个分裂: nnoremap <C-J> <C-W><C-J> nnoremap <C-K> <C-W><C-K> nnoremap <C-L> <C-W><C-L> nnoremap <C-H> <C-W><C-H>

其他回答

像:e和:badd这样的东西只接受一个参数,因此下面的将会失败

:e foo.txt bar.txt
:e /foo/bar/*.txt
:badd /foo/bar/*

如果您想从vim中添加多个文件,请使用arga[dd]

:arga foo.txt bar.txt
:arga /foo/bar/*.txt
:argadd /foo/bar/*

我有效处理多个文件的方法是使用tmux。

它允许你垂直和水平分割窗口,如下所示:

I have it working this way on both my mac and linux machines and I find it better than the native window pane switching mechanism that's provided (on Macs). I find the switching easier and only with tmux have I been able to get the 'new page at the same current directory' working on my mac (despite the fact that there seems to be options to open new panes in the same directory) which is a surprisingly critical piece. An instant new pane at the current location is amazingly useful. A method that does new panes with the same key combos for both OS's is critical for me and a bonus for all for future personal compatibility. Aside from multiple tmux panes, I've also tried using multiple tabs, e.g. and multiple new windows, e.g. and ultimately I've found that multiple tmux panes to be the most useful for me. I am very 'visual' and like to keep my various contexts right in front of me, connected together as panes.

Tmux还支持水平和垂直窗格,这是旧屏幕所不支持的(尽管mac的iterm2似乎支持它,但是,当前的目录设置对我来说不管用)。tmux 1.8

我做了一个非常简单的视频来展示我使用的工作流程。基本上我使用Ctrl-P Vim插件,并将缓冲区导航映射到Enter键。

通过这种方式,我可以在普通模式下按Enter,查看打开的文件列表(在屏幕底部的一个小新窗口中显示),选择我想编辑的文件并再次按Enter。要快速搜索多个打开的文件,只需输入部分文件名,选择文件并按Enter。

视频中打开的文件不多,但当你开始有很多文件时,它会变得非常有用。

由于插件使用MRU顺序对缓冲区进行排序,您只需按两次Enter键就可以跳转到您正在编辑的最新文件。

插件安装完成后,你唯一需要做的配置是:

nmap <CR> :CtrlPBuffer<CR>

当然,您可以将它映射到不同的键,但我发现要输入的映射非常方便。

这个线程中的一些答案建议使用制表符,其他建议使用buffer来完成同样的事情。制表符和缓冲区是不同的。我强烈建议你阅读这篇文章“Vim标签疯狂-缓冲区vs标签”。

下面是我从这篇文章中摘录的一个很好的总结:

简介:

缓冲区是文件在内存中的文本。 窗口是缓冲区上的视口。 标签页是窗口的集合。

我认为您在查看已打开的文件列表时可能使用了错误的命令。

尝试执行一个:ls来查看你已经打开的文件列表,你会看到:

   1 %a   "./checkin.pl"            line 1
  2 #    "./grabakamailogs.pl"     line 1
  3      "./grabwmlogs.pl"         line 0
  etc.

然后,您可以通过引用列出的数字来浏览文件,例如: b: 3

或者你可以通过输入数字,但使用sb而不是b来分割屏幕。

作为旁白,%表示当前可见的文件,#表示备用文件。

您可以通过按Ctrl Shift 6在这两个文件之间轻松切换

编辑:像:ls,你可以使用:reg来查看你的寄存器的当前内容,包括0-9寄存器,其中包含你已经删除的内容。如果您想重新使用以前删除的文本,这尤其有用。