Accessing Multiple Files in VIM

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.

Upvotes: 13

Views: 8893

Answers (5)

darkfeline
darkfeline

Reputation: 10652

To clarify, Vim has the argument list, the buffer list, windows, and tab pages. The argument list is the list of files you invoked vim with (e.g. vim file1 file2); the :n and :p commands work with this. The buffer list is the list of in-memory copies of the files you are editing, just like emacs. Note that all the files loaded at start (in the argument list) are also in the buffer list. Try :help buffer-list for more information on both.

Windows are viewports for buffers. Think of windows as "desks" on which you can put buffers to work on them. Windows can be empty or be displaying buffers that can also be displayed in other windows, which you can use for example to look at two different areas of the same buffer at the same time. Try :help windows for more info.

Tabs are collections of windows. For example, you can have one tab with one window, and another tab with two windows vertically split. Try :help tabpage for more info.

Upvotes: 4

Rook
Rook

Reputation: 62538

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

Vim has buffers. You open every file with:

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

You can also:

:e *.txt

Useful options while doing this are

: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) enable you to cycle through buffers

:bd closes the buffer/file (buffer done)

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

Upvotes: 18

Brian Carper
Brian Carper

Reputation: 72926

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.

Upvotes: 9

DanM
DanM

Reputation: 2391

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.

Upvotes: 5

Jonathan Leffler
Jonathan Leffler

Reputation: 753655

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.

Upvotes: 5

Related Questions