tags:

views:

6922

answers:

11

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 e.g. "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?

+3  A: 

vim (but not vi!) has tabs which I find far superior to buffers. You can say :tabe [filename] to open a file in a new tab. Cycling between tabs is done by klicking on the tab or by the key combinatons [n]gt and gT. Graphical vim even has graphical tabs.

Konrad Rudolph
Thanks, sounds great, but we unfortunately only have VIM 6.1 installed on the server.
Edward Tanguay
+13  A: 

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 :(w)q you close a tab. If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.

If it are not that many files and 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 a arrow key in the direction you want to move.

D4V360
Very nice, just spent 10 minutes learning about spliting screens and that is exactly what I need only need 2-3 files open at one time and they are all labeled on my screen, thanks!
Edward Tanguay
To save and close a tab, you can also use `ZZ` instead of `:wq` (just like would normally save and close)
Andreas Grech
Lovely! Thanks.
Andrejs Cainikovs
+5  A: 

To add to the args list: :argadd

To delete from the args list: :argdelete

In your example you could use :argedit test.pl to add test.pl to the args list and edit the file in one step.

:help args gives much more detail and advanced usage

MarkB
+4  A: 

I think you may be using the wrong command for looking at the list of files that you have open.

Try doing an :ls to see the list of files that you have open and you'll see:

   1 %a   "./checkin.pl"            line 1
  2 #    "./grabakamailogs.pl"     line 1
  3      "./grabwmlogs.pl"         line 0
  etc.

You can then bounce through the files by referring to them by the numbers listed, e.g. :3b

or you can split your screen by entering the number but using sb instead of just b.

As an aside % refers to the file currently visible and # refers to the alternate file.

You can easily toggle between these two files by pressing

<cntl>-shift-6

Edit: like :ls you can use :reg to see the current contents of your registers including the 0-9 registers that contain what you've deleted. This is especially useful if you want to reuse some text that you've previously deleted.

HTH

cheers,

Rob

Rob Wells
+4  A: 

I use buffer commands - :bn (next buffer), :bp (previous buffer) :buffers (list open buffers) :b<n> (open buffer n) :bd (delete buffer). :e <filename> will just open into a new buffer.

Andy Whitfield
:ls faster than :buffers
Wahnfrieden
+11  A: 

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 choses the last visited file by default, so you can use it to switch quickly between two files. I use it a lot.


Using windows

Ctrl-W s and Ctrl-W v to split the current window horizontally and vertically.

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


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.

Cheers!

Sébastien RoccaSerra
A: 

If you are going to use multiple buffers, I think the most important thing is to set hidden so that it will let you switch buffers even if you have unsaved changes in the one you are leaving.

indentation
A: 

When using multiple files in vim, I use these commands mostly (with ~350 files open):

  • :b (jump to a buffer)
  • :bw (buffer wipe, remove a buffer)
  • :e (edit, open a new buffer>
  • pltags - enable jumping to subroutine/method definitions
zigdon
+4  A: 
:ls

for list of open buffers

  • :bp previous buffer
  • :bn next buffer
  • :bn move to nth buffer
  • :b with tab-key providing auto-completion (awesome !!)

or when u are in normal mode ^ to switch to the last file u were working on

plus, you can save sessions of vim

:mksession! ~/today.ses

saves the current open files buffers and settings to ~/today.ses. u can load that session by using

vim -S ~/today.ses

no hassle of remembering where u left of the yesterday ;)

shyam
+1  A: 

I use multiple buffers

set hidden in my vimrc

The mini-buffer explorer script is nice too to get a nice compact listing of your buffers. Then :b1 or :b2... to go to the appropriate buffer or use the mini-buffer explorer and tab through the buffers.

projecktzero
+1  A: 

I use the same .vimrc file for GVim and the command line vim. I tend to use tabs in GVim and buffers in the command line vim, so I have my .vimrc set up to make working with both of them easier:

" 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

This clobbers the existing mappings for H and L, but it makes switching between files extremely fast and easy. Just hit "H" for next and "L" for previous; whether you're using tabs or buffers, you'll get the intended results.

Max
I like these mappings. Also try Ctrl-H, Ctrl-L. I setup Firefox and gnome terminal with the same mappings. Very nice to have consistent tab key shortcuts.
Casey
use your leader key instead of ctrl to avoid clobbering existing mappings
Wahnfrieden