How to effectively work with multiple files in Vim?

I've started using Vim to develop Perl scripts and am starting to find it very powerful.

One thing I like is to be able to open multiple files at once with eg vi main.pl maintenance.pl and then hop between them with:

:n
:prev

and see which file are open with

:args

And to add a file, I can say:

:n test.pl

which I expect would then be ADDED to my list of files, but instead it wipes out my current file list and when I type :args I only have test.pl open.

So how can I ADD and REMOVE files to my args list?


Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp , With :tabe <filepath> you can add a new tab; and with a regular :q or :wq you close a tab. If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.

If there are not that many files or you don't have Vim 7 you can also split your screen in multiple files: :sp <filepath> . Then you can switch between splitscreens with Ctrl+W and then an arrow key in the direction you want to move (or instead of arrow keys, w for next and W for previous splitscreen)


Listing

To see a list of current buffers, I use:

:ls

Opening

To open a new file, I use

:e ../myFile.pl

with enhanced tab completion (put set wildmenu in your .vimrc ).

Note: you can also use :find which will search a set of paths for you, but you need to customize those paths first.


Switching

To switch between all open files, I use

:b myfile

with enhanced tab completion (still set wildmenu ).

Note: :b# chooses the last visited file, so you can use it to switch quickly between two files.


Using windows

Ctrl-W s and Ctrl-W v to split the current window horizontally and vertically. You can also use :split and :vertical split ( :sp and :vs )

Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l ) to navigate through open windows.

Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one.

Starting vim with a -o or -O flag opens each file in its own split.


With all these I don't need tabs in Vim, and my fingers find my buffers, not my eyes.

Note: if you want all files to go to the same instance of Vim, start Vim with the --remote-silent option.


:ls

for list of open buffers

  • :bp previous buffer
  • :bn next buffer
  • :bn ( n a number) move to n'th buffer
  • :b <filename-part> with tab-key providing auto-completion (awesome !!)
  • In some versions of vim, bn and bp are actually bnext and bprevious respectively. Tab auto-complete is helpful in this case.

    Or when you are in normal mode, use ^ to switch to the last file you were working on.

    Plus, you can save sessions of vim

    :mksession! ~/today.ses
    

    The above command saves the current open file buffers and settings to ~/today.ses . You can load that session by using

    vim -S ~/today.ses
    

    No hassle remembering where you left off yesterday. ;)

    链接地址: http://www.djcxy.com/p/3860.html

    上一篇: 我如何使用vim寄存器?

    下一篇: 如何在Vim中有效地处理多个文件?