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

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

vi main.pl maintenance.pl

然后在他们之间跳来跳去

:n
:prev

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

:args

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

:n test.pl

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

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


当前回答

您可能希望使用Vim全局标记。

通过这种方式,您可以快速地在文件之间来回切换,甚至跳转到文件中标记的位置。此外,关键命令也很短: C会带我找到我正在处理的代码, T把我带到了我正在使用的单元测试。

当你改变位置时,重置标记也很快: mC标记新的代码点, mT标志着新的测试点。

其他回答

如果只使用vim内置命令,我所见过的在多个缓冲区之间切换的最好的命令是:

nnoremap <Leader>f :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>

它完美地结合了:ls和:b命令——列出所有打开的缓冲区,并等待您输入切换缓冲区的命令。

给定vimrc中的上述映射,一旦输入<Leader>f,

显示所有打开的缓冲区 您可以: 输入23进入缓冲区23, 输入#进入替代/MRU缓冲区, 输入文件的部分名称,然后输入<Tab>,或<C-i>来自动补全, 或者只是<CR>或<Esc>来保持在当前缓冲区

上面键映射的输出快照如下:

:set nomore|:ls|:set more
  1  h    "script.py"    line 1
  2 #h  + "file1.txt"    line 6  -- '#' for alternative buffer
  3 %a    "README.md"    line 17 -- '%' for current buffer
  4       "file3.txt"    line 0  -- line 0 for hasn't switched to
  5     + "/etc/passwd"  line 42 -- '+' for modified
:b '<Cursor> here'

在上图中:

第二列:%a为当前,h为隐藏,#为以前,空为尚未切换到。 第三列:+表示修改。

另外,我强烈建议设置隐藏。看:帮助“隐藏”。

这里有很多答案!我不用重新发明轮子——最著名的插件(不会很快消亡,被很多人使用)是超快和极客的。

ctrlpvim / ctrlp。Vim -查找文件的名称模糊搜索的位置或只是它的名称 jlanzarotta/bufexplorer -用来浏览打开的缓冲区(当你不记得最近打开和修改了多少文件,也不记得它们在哪里时,可能是因为你用Ag搜索了它们) rk / ag)。Vim搜索关于gitignore的文件 Scrooloose /nerdtree查看目录结构,查找,添加/删除/修改文件

编辑:最近我一直在使用dyng/ctrlsf。vim搜索上下文视图(像Sublime搜索),我把引擎从ag切换到ripgrep。演出非常出色。

EDIT2:与CtrlSF一起,您可以使用mg979/vim-visual-multi,一次更改多个文件,然后在最后一次保存它们。

你可以在你的.bashrc文件中添加vim和vim -p的别名:

alias vim="vim -p"

这将导致在选项卡中从shell打开多个文件,而不必随后在vim中调用:tab ball。

Vim(但不是原始的Vi!)有制表符,我发现(在许多情况下)比缓冲区更好。你可以说:table [filename]在新选项卡中打开一个文件。标签之间的循环是通过点击标签或组合键[n]gt和gt来完成的。graphic Vim甚至有图形标签。

我有效处理多个文件的方法是使用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