tags:

views:

841

answers:

4

The book "Unix in a Nutshell" discusses about accessing multiple files on pages 572-573. There seem to be very useful commands such as ":e", ":e #", ":e new_file", ":n files", ":args", ":prev" and ":n!". The commands confuse me:

":n Edit next file in the list of files."

":args Display list of files to be edited."

":prev Edit previous file in the list of files."

I cannot see no real list when I do ":args". There is only a small text at the corner. I would like to see all files that I accessed with ":e", ie a list of files in the buffer.

Where can I see the list when I do the command ":n files"? What are the commands ":prev" and ":n" supposed to do? I got the error message:

There is only one file to edit.

+4  A: 

For those commands to make sense, you do:

vim *.c

in a directory where there are twenty C files, for example. With a single file, there is no next or previous or significant list of files.

Jonathan Leffler
Wow! Very nice example :) Great thanks!
Masi
+4  A: 

I've not read the book in mention, but I'll try to explain how vim handles files.

Vim has buffers (several kinds of them, but lets leave that for now). Every file you open with:

:e name_of_the_file.txt (loads file in buffer, e.g. "opens file")

You can also:

:e *.txt

Useful options while doing this is

:set laststatus=2 (to always show the statusline)
:set wildmenu (to ease opening files)

If you have standard vim with netrw plugin, you can:

:e . (for one of vim's file managers, so to say)

To manage buffers:

:ls will give you a list of currently opened buffers (files)
:bnext, and bprevious, or :bn and :bp will enable you to go "through" buffers
:bd will close the buffer/file (buffer done)

Other buffer type serve other purposes (yanking/pasting, temporary, vim's internal, ... etc.)

ldigas
Is the notation of buffer the same as in Emacs? Interestingly, the book defines buffer only for Emacs :( It states "When you open a file in Emacs, the file is put into a Buffer. -- The view of the buffer contents that you have at any point in time is called a window." Are the buffers and windows different to the things in Vim?
Masi
Yes, you could say that. There are some differences in types of available buffers, but in principle, that's it. I'm not sure about emacs, he has windows/frames .., while vim has windows/tabs. Regarding vim: a window is only y method of showing what vim has in a buffer. A tab is a method of showing several windows on screen (tabs in vim have only recently been introduced).
ldigas
+3  A: 

The :n :p :ar :rew :last operate on the command line argument list.

E.g.

> touch aaa.txt bbb.txt ccc.txt
> gvim *.txt

vim opens in aaa.txt

:ar gives a status line

[aaa.txt] bbb.txt ccc.txt

:n moves to bbb.txt

:ar gives the status line

aaa.txt [bbb.txt] ccc.txt

:rew rewinds us back to the start of the command line arg list to aaa.txt

:last sends us to ccc.txt

:e ddd.txt edits a new file ddd.txt

:ar gives the status line

aaa.txt bbb.txt [ccc.txt]

So the command set only operates on the initial command line argument list.

DanM
+1 for the :last-command.
Masi
+1  A: 

In addition to what Jonathan Leffler said, if you don't invoke Vim with multiple files from the commandline, you can set Vim's argument list after Vim is open via:

:args *.c

Note that the argument list is different from the list of open buffers you get from :ls. Even if you close all open buffers in Vim, the argument list stays the same. :n and :prev may open a brand new buffer in Vim (if a buffer for that file isn't already open), or may take you to an existing buffer.

Similarly you can open multiple buffers in Vim without affecting the argument list (or even if the arg list is empty). :e opens a new buffer but doesn't necessarily affect the argument list. The list of open buffers and the argument list are independent. If you want to iterate through the list of open buffers rather than iterate through the argument list, use :bn and :bp and friends.

Brian Carper