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 43. Explore the File System with netrw

Tip 43. Explore the File System with netrw

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

Explore the File System with netrw

• 99

Figure 2—netrw—Vim’s “native” file explorer

For example, if we wanted to open the index.html file, we could search for

/html , putting our cursor right where we need it.

Opening the File Explorer

We can open the file explorer window with the :edit {path} command by supplying a directory name (instead of a filename) as the {path} argument. The dot

symbol stands for the current working directory, so if we run the :edit . command, we can bring up a file explorer for the project root.

If we wanted to open a file explorer for the directory of the current file, we

could do so by typing :edit %:h (see Open a File Relative to the Active File

Directory, on page 95, for an explanation). But the netrw plugin provides a

more convenient way with the :Explore command (see :h :Explore ).

Both of these commands can be abbreviated. Instead of typing out :edit ., we

can get away with just :e.—we don’t even need the space before the dot. And

:Explore can be truncated right down to :E. This table summarizes the longand shorthand forms of these commands:

Ex Command



:edit .


Open file explorer for current working directory



Open file explorer for the directory of the active buffer

In addition to :Explore, netrw also provides :Sexplore and :Vexplore commands,

which open the file explorer in a horizontal split window or vertical split window, respectively.

report erratum • discuss

Chapter 7. Open Files and Save Them to Disk

• 100

Working with Split Windows

The classic GUI for a text editor presents the file explorer in a sidebar,

sometimes known as the project drawer. If you’re used to this kind of interface,

then it might seem strange that Vim’s :E and :e. commands behave the way

they do by replacing the contents of the current window with a file explorer.

There’s a good reason for this: it works well with split windows.

Consider the layout in the first frame of this image:


















Here we see three split windows, each displaying a different buffer. Let’s

imagine for a moment that a project drawer containing a file explorer was

bolted to the side of Vim’s interface. If we want to open a file by clicking its

name in the project drawer, where would it open?

The window labeled C is active (as indicated by the shading), so that would

seem to be the natural target. But the relationship between the project

drawer and the active window is not immediately apparent. It would be easy

to lose track of which window was active, leading to a surprise result when,

on selecting a file from the project drawer, it didn’t open where you expected

it to.

Now let’s remove our imaginary project drawer from this scenario and consider

the way it actually works in Vim. If we run the :Explore command, the active

window is replaced with a file explorer, as illustrated by frame 2 of the figure.

There can be no doubt that when a file is selected it will load in the same


Think of each window as a playing card. One side of the card shows the contents of a file, and the other side shows the file explorer. When we run the

:Explore command, the card for the active window flips over to show the side

with the file explorer (frame 2 of the figure). After choosing the file we want

to edit, we press and the card flips over again, this time showing the

contents of the file that we just selected (frame 3 of the figure). After summoning the file explorer view, if we decide that we want to switch back to the

buffer we were already editing, we can do so using the command.

report erratum • discuss

Save Files to Nonexistent Directories

• 101

In a sense, we could say that Vim’s windows have two modes: one for working

with files and one for working with directories. This model works together

with Vim’s split window interface perfectly, whereas the notion of a project

drawer doesn’t really fit.

Doing More with netrw

The netrw plugin doesn’t just let us explore the file system. We can create

new files (:h netrw-% ) or directories (:h netrw-d ), rename existing ones

(:h netrw-rename ), or delete them (:h netrw-del ). For a demonstration, watch

episode 15 of Vimcasts.1

We haven’t even touched on the killer feature that gives the plugin its name:

netrw makes it possible to read and write files across a network. The plugin

can use many protocols, including scp, ftp, curl, and wget, depending on what’s

available on your system. To find out more, look up :h netrw-ref .

Tip 44

Save Files to Nonexistent Directories

Vim is happy to let us edit a buffer whose path includes directories that don’t

exist. It’s only when we attempt to write the buffer to a file that Vim objects.

Here’s a quick tip on how to deal with this situation.

The :edit {file} command is most commonly used to open a file that already

exists. But if we specify a filepath that doesn’t correspond to an existing file,

then Vim will create a new empty buffer. If we press , we’ll see that the

buffer is labeled as “new file” (the command echoes the name and status

of the current file; see :h ctrl-G ). When we run the :write command, Vim will

attempt to write the contents of that buffer to a new file using the filepath

that was specified when the buffer was created.

If we run :edit {file} and specify a filepath that contains nonexistent directories,

things can get a little awkward:

:edit madeup/dir/doesnotexist.yet


"madeup/dir/doesnotexist.yet" E212: Can't open file for writing

In this case, the madeup/dir directories do not exist. Vim creates a new buffer

anyway, but this time it’s labeled as “new DIRECTORY.” It’s only when we


report erratum • discuss

Chapter 7. Open Files and Save Them to Disk

• 102

attempt to write the buffer to disk that Vim raises an error. We can remedy

this situation by calling the external mkdir program:

:!mkdir -p %:h


The -p flag tells mkdir to create intermediate directories. See Open a File Relative

to the Active File Directory, on page 95, for an explanation of what the %:h

characters stand for.

Tip 45

Save a File as the Super User

Running Vim as the super user isn’t normal, but sometimes we have to save

changes to a file that requires sudo permission. We can do so without restarting

Vim by delegating the task to a shell process and running that with sudo.

This tip may not work in GVim and certainly won’t work on Windows. It does

work on Unix systems when you run Vim inside a terminal, which is a common

enough scenario to make this tip worthy of inclusion.

Let’s use the /etc/hosts file to demonstrate. The file is owned by root, but we’re

logged in with username “drew,” so we have permission only to read this file:

$ ls -al /etc/ | grep hosts

-rw-r--r-1 root wheel

$ whoami



6 Apr 15:59 hosts

We’ll open up the file in Vim as user drew:

$ vim /etc/hosts

The first thing to note is that if we press to view the file status, Vim

labels it as [readonly].

Let’s try to make a change and see what happens. We’ll run the Go commands

to add a blank line at the end of the file. Vim echoes a message that reads

“W10: Warning: Changing a readonly file.” Consider this a friendly reminder

rather than an absolute rule. After showing the message, Vim proceeds by

making the change anyway.

Vim won’t prevent us from making changes to a readonly buffer, but it will

prevent us from saving the changes to disk in the usual manner:

report erratum • discuss

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

Tip 43. Explore the File System with netrw

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