Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 36. Track Open Files with the Buffer List

Tip 36. Track Open Files with the Buffer List

Tải bản đầy đủ - 0trang

Chapter 6. Manage Multiple Files

• 78

Files are stored on the disk, whereas buffers exist in memory. When we open

a file in Vim, its contents are read into a buffer, which takes the same name

as the file. Initially, the contents of the buffer will be identical to those of the

file, but the two will diverge as we make changes to the buffer. If we decide

that we want to keep our changes, we can write the contents of the buffer

back into the file. Most Vim commands operate on buffers, but a few operate

on files, including the :write, :update, and :saveas commands.

Meet the Buffer List

Vim allows us to work with multiple buffers simultaneously. Let’s open a few

files by running these commands in the shell:

$ cd code/files

$ vim *.txt

2 files to edit

The *.txt wildcard matches two files in the current directory: a.txt and b.txt. This

command tells Vim to open both of those files. When Vim starts up, it shows

a single window with a buffer representing the first of the two files. The other

file isn’t visible, but it has been loaded into a buffer in the background, as

we can see by running the following:


1 %a




line 1

line 0

The :ls command gives us a listing of all the buffers that have been loaded

into memory (:h :ls ). We can switch to the next buffer in the list by running

the :bnext command:



1 #


2 %a


line 1

line 1

The % symbol indicates which of the buffers is visible in the current window,

while the # symbol represents the alternate file. We can quickly toggle between

the current and alternate files by pressing . Press it once, and we’ll switch

to a.txt; press it a second time, and we’ll get back to b.txt.

Use the Buffer List

We can traverse the buffer list using four commands—:bprev and :bnext to move

backward and forward one at a time, and :bfirst and :blast to jump to the start

or end of the list. It’s a good idea to map them to something easier to reach.

report erratum • discuss

Track Open Files with the Buffer List

• 79

See Create Mappings to Quickly Traverse Vim's Lists, on page 79, for a suggested mapping.

The :ls listing starts with a digit, which is assigned to each buffer automatically

on creation. We can jump directly to a buffer by number, using the :buffer N

command (see :h :b ). Alternatively, we can use the more intuitive form, :buffer

{bufname}. The {bufname} need only contain enough characters from the filepath

to uniquely identify the buffer. If we enter a string that matches more than

one of the items in the buffer list, we can use tab-completion to choose between

the options (see Tip 32, on page 65).

The :bufdo command allows us to execute an Ex command in all of the buffers

listed by :ls (:h :bufdo ). In practice, I usually find that it’s more practical to

use :argdo instead, which we’ll meet in Tip 37, on page 80.

Create Mappings to Quickly Traverse Vim’s Lists

Typing :bn and :bp can feel laborious. To speed things up a bit, I use these mappings

from Tim Pope’s unimpaired.vim plugin:a













Vim already uses the [ and ] keys as prefixes for a series of related commands (see

:h [ ), so these mappings have a consistent feel to them. The unimpaired.vim plugin

provides similar mappings for scrolling through the argument ( [a and ]a ), quickfix

( [q and ]q ), location ( [l and ]l ), and tag lists ( [t and ]t ). Check it out.


Deleting Buffers

Vim creates a new buffer any time we open a file, and we’ll learn a few ways

of doing that in Chapter 7, Open Files and Save Them to Disk, on page 93. If

we want to delete a buffer, we can do so using the :bdelete command. This can

take one of these forms:

:bdelete N1 N2 N3

:N,M bdelete

Note that deleting a buffer has no effect on its associated file; it simply removes

the in-memory representation. If we wanted to delete buffers numbered 5

through 10 inclusive, we could do so by running :5,10bd. But if we wanted to

keep buffer number 8, then we’d instead have to run :bd 5 6 7 9 10.

report erratum • discuss

Chapter 6. Manage Multiple Files

• 80

Buffer numbers are automatically assigned by Vim, and we have no means

of changing them by hand. So if we want to delete one or more buffers, we

first have to look them up to find out their numbers. This procedure is

relatively time-consuming. Unless I have a good reason to delete a buffer, I

usually don’t bother. As a result, the :ls listing comes to represent all of the

files that I have opened in the course of an editing session.

Vim’s built-in controls for managing the buffer list lack flexibility. If we want

to arrange buffers in a way that makes sense for our workflow, attempting to

organize the buffer list is not the way to go. Instead, we’re better off dividing

our workspace using split windows, tab pages, or the argument list. The next

few tips will show how.

Tip 37

Group Buffers into a Collection with the Argument List

The argument list is easily managed and can be useful for grouping together

a collection of files for easy navigation. We can run an Ex command on each

item in the argument list using the :argdo command.

Let’s start by opening a handful of files in Vim:

$ cd code/files/letters

$ vim *.txt

5 files to edit

In Tip 36, on page 77, we saw that the :ls command provides a listing of buffers.

Now let’s examine the argument list:


[a.txt] b.txt c.txt. d.txt e.txt

The argument list represents the list of files that was passed as an argument

when we ran the vim command. In our case, we provided a single argument,

*.txt, but our shell expanded the * wildcard, matching the five files that we see

in our argument list. The [] characters indicate which of the files in the

argument list is active.

Compared to the listing provided by the :ls command, the output from running

:args looks crude. It should come as no surprise to learn that the argument

list was a feature of vi, whereas the buffer list is an enhancement introduced

by Vim. But give the argument list a chance, and you’ll see that it makes a

fine complement to the buffer list.

report erratum • discuss

Group Buffers into a Collection with the Argument List

• 81

Like many features in Vim, the functionality of the argument list has been

enhanced, while the original name has stuck. We can change the contents

of the argument list at any time, which means that the :args listing doesn’t

necessarily reflect the values that were passed to the vim command when we

launched the editor. Don’t take the name literally! (Also, see ‘:compiler’ and

‘:make’ Are Not Just for Compiled Languages, on page 268.)

Populate the Argument List

When the :args Ex command is run without arguments, it prints the contents

of the argument list. We can also set the contents of the argument list using

this form (:h :args_f ):

:args {arglist}

The {arglist} can include filenames, wildcards, or even the output from a shell

command. To demonstrate, we’ll use the files/mvc directory, which you can find

in the source files that come distributed with this book. If you want to follow

along, switch to that directory and launch Vim:

$ cd code/files/mvc

$ vim

For an overview of the directory tree, refer to the code listing on page 94.

Specify Files by Name

The simplest way of populating the argument list is by specifying filenames

one by one:

:args index.html app.js


[index.html] app.js

This technique works fine if we only want to add a handful of buffers to our

set. It has the advantage that we can specify the order, but doing it by hand

can be laborious. If we want to add a lot of files to the argument list, we can

get the job done faster by using wildcards.

Specify Files by Glob

Wildcards are placeholders that can stand in for characters in the name of a

file or directory. The * symbol will match zero or more characters, but only

in the scope of the specified directory (:h wildcard ). The ** wildcard also

matches zero or more characters, but it can recurse downward into directories

below the specified directory (:h starstar-wildcard ).

report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 36. Track Open Files with the Buffer List

Tải bản đầy đủ ngay(0 tr)