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

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

vi main.pl maintenance.pl

然后在他们之间跳来跳去

:n
:prev

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

:args

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

:n test.pl

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

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


当前回答

当在vim中使用多个文件时,我主要使用这些命令(打开了大约350个文件):

:b <partial filename><tab>(跳转到缓冲区) :bw(缓冲区擦除,删除缓冲区) :e <文件路径>(编辑,打开一个新的缓冲区> . Pltags -允许跳转到子例程/方法定义

其他回答

如果只使用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为隐藏,#为以前,空为尚未切换到。 第三列:+表示修改。

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

如果你要使用多个缓冲区,我认为最重要的事情是 设置隐藏 这样它就能让你切换缓冲区即使你要离开的缓冲区中有未保存的更改。

这个帖子中的大多数答案都是使用普通的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>

当在vim中使用多个文件时,我主要使用这些命令(打开了大约350个文件):

:b <partial filename><tab>(跳转到缓冲区) :bw(缓冲区擦除,删除缓冲区) :e <文件路径>(编辑,打开一个新的缓冲区> . Pltags -允许跳转到子例程/方法定义

When I started using VIM I didn't realize that tabs were supposed to be used as different window layouts, and buffer serves the role for multiple file editing / switching between each other. Actually in the beginning tabs are not even there before v7.0 and I just opened one VIM inside a terminal tab (I was using gnome-terminal at the moment), and switch between tabs using alt+numbers, since I thought using commands like :buffers, :bn and :bp were too much for me. When VIM 7.0 was released I find it's easier to manager a lot of files and switched to it, but recently I just realized that buffers should always be the way to go, unless one thing: you need to configure it to make it works right.

所以我尝试了vim-airline,并启用了可视的顶部选项卡式缓冲条,但图形与我的iTerm2有问题,所以我尝试了其他几个,似乎MBE最适合我。我还设置shift+h/l作为快捷键,因为原来的那些(移动到当前页面的头部/尾部)对我来说不是很有用。 map <S-h>:bprev<返回> map <S-l>:bnext<返回>

它似乎比gt和gt更简单,而且:e也比:tabnew更简单。我发现:bd不像:q那样方便,虽然(MBE有一些问题),但我可以在缓冲区中使用所有文件。