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

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

vi main.pl maintenance.pl

然后在他们之间跳来跳去

:n
:prev

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

:args

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

:n test.pl

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

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


当前回答

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

清单

要查看当前缓冲区的列表,我使用:

:ls

开放

要打开一个新文件,我使用

:e ../myFile.pl

增强的TAB补全功能(将set wildmenu放在你的.vimrc中)。

注意:你也可以使用:find,它会为你搜索一组路径,但是你需要先自定义这些路径。


切换

要在所有打开的文件之间切换,我使用

:b myfile

增强选项卡完成(仍然设置野生菜单)。

注意::b#选择最后访问的文件,因此您可以使用它在两个文件之间快速切换。


使用windows

Ctrl-W s和Ctrl-W v水平和垂直分割当前窗口。你也可以使用:split和:vertical split (:sp和:vs)

Ctrl-W w在打开的窗口之间切换,Ctrl-W h(或j或k或l)在打开的窗口中导航。

按Ctrl-W c关闭当前窗口,按Ctrl-W o关闭除当前窗口外的所有窗口。

使用-o或-o标志启动vim将打开每个文件各自的分割。


有了所有这些,我不需要在Vim中使用标签,我的手指可以找到缓冲区,而不是眼睛。

注意:如果您希望所有文件都转到同一个Vim实例,请使用——remote-silent选项启动Vim。

我使用以下,这给了你很多功能,你会期望在其他编辑器,如Sublime文本/ Textmate

Use buffers not 'tab pages'. Buffers are the same concept as tabs in almost all other editors. If you want the same look of having tabs you can use the vim-airline plugin with the following setting in your .vimrc: let g:airline#extensions#tabline#enabled = 1. This automatically displays all the buffers as tab headers when you have no tab pages opened Use Tim Pope's vim-unimpaired which gives [b and ]b for moving to previous/next buffers respectively (plus a whole host of other goodies) Have set wildmenu in your .vimrc then when you type :b <file part> + Tab for a buffer you will get a list of possible buffers that you can use left/right arrows to scroll through Use Tim Pope's vim-obsession plugin to store sessions that play nicely with airline (I had lots of pain with sessions and plugins) Use Tim Pope's vim-vinegar plugin. This works with the native :Explore but makes it much easier to work with. You just type - to open the explorer, which is the same key as to go up a directory in the explorer. Makes navigating faster (however with fzf I rarely use this) fzf (which can be installed as a vim plugin) is also a really powerful fuzzy finder that you can use for searching for files (and buffers too). fzf also plays very nicely with fd (a faster version of find) Use Ripgrep with vim-ripgrep to search through your code base and then you can use :cdo on the results to do search and replace

如果只使用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,一次更改多个文件,然后在最后一次保存它们。