我已经开始使用Vim来开发Perl脚本,并开始发现它非常强大。
我喜欢的一件事是能够同时打开多个文件:
vi main.pl maintenance.pl
然后在他们之间跳来跳去
:n
:prev
然后查看哪个文件是打开的:
:args
要添加一个文件,我可以说:
:n test.pl
我希望它会被添加到我的文件列表中,但相反,它会清除我当前的文件列表,当我输入:args时,我只有test.pl打开。
那么我如何在我的args列表中添加和删除文件?
我已经开始使用Vim来开发Perl脚本,并开始发现它非常强大。
我喜欢的一件事是能够同时打开多个文件:
vi main.pl maintenance.pl
然后在他们之间跳来跳去
:n
:prev
然后查看哪个文件是打开的:
:args
要添加一个文件,我可以说:
:n test.pl
我希望它会被添加到我的文件列表中,但相反,它会清除我当前的文件列表,当我输入:args时,我只有test.pl打开。
那么我如何在我的args列表中添加和删除文件?
当前回答
我对gVim和命令行Vim使用相同的.vimrc文件。我倾向于在gVim中使用制表符,在命令行Vim中使用缓冲区,所以我设置了.vimrc,以便更容易地使用它们:
" Movement between tabs OR buffers
nnoremap L :call MyNext()<CR>
nnoremap H :call MyPrev()<CR>
" MyNext() and MyPrev(): Movement between tabs OR buffers
function! MyNext()
if exists( '*tabpagenr' ) && tabpagenr('$') != 1
" Tab support && tabs open
normal gt
else
" No tab support, or no tabs open
execute ":bnext"
endif
endfunction
function! MyPrev()
if exists( '*tabpagenr' ) && tabpagenr('$') != '1'
" Tab support && tabs open
normal gT
else
" No tab support, or no tabs open
execute ":bprev"
endif
endfunction
这破坏了H和L的现有映射,但它使文件之间的切换非常快速和容易。按H表示下一个,按L表示上一个;无论您使用的是制表符还是缓冲区,都将得到预期的结果。
其他回答
Vim(但不是原始的Vi!)有制表符,我发现(在许多情况下)比缓冲区更好。你可以说:table [filename]在新选项卡中打开一个文件。标签之间的循环是通过点击标签或组合键[n]gt和gt来完成的。graphic Vim甚至有图形标签。
我使用缓冲区命令-:bn(下一个缓冲区),:bp(上一个缓冲区):buffers(列表打开缓冲区):b<n>(打开缓冲区n):bd(删除缓冲区)。:e <filename>将打开一个新的缓冲区。
我对gVim和命令行Vim使用相同的.vimrc文件。我倾向于在gVim中使用制表符,在命令行Vim中使用缓冲区,所以我设置了.vimrc,以便更容易地使用它们:
" Movement between tabs OR buffers
nnoremap L :call MyNext()<CR>
nnoremap H :call MyPrev()<CR>
" MyNext() and MyPrev(): Movement between tabs OR buffers
function! MyNext()
if exists( '*tabpagenr' ) && tabpagenr('$') != 1
" Tab support && tabs open
normal gt
else
" No tab support, or no tabs open
execute ":bnext"
endif
endfunction
function! MyPrev()
if exists( '*tabpagenr' ) && tabpagenr('$') != '1'
" Tab support && tabs open
normal gT
else
" No tab support, or no tabs open
execute ":bprev"
endif
endfunction
这破坏了H和L的现有映射,但它使文件之间的切换非常快速和容易。按H表示下一个,按L表示上一个;无论您使用的是制表符还是缓冲区,都将得到预期的结果。
清单
要查看当前缓冲区的列表,我使用:
: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。
这个帖子中的大多数答案都是使用普通的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>