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 41. Open a File by Its Filepath Using ‘:edit’

Tip 41. Open a File by Its Filepath Using ‘:edit’

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

Chapter 7. Open Files and Save Them to Disk

• 94

















In the shell, we’ll start by changing to the files/mvc directory and then launching


$ cd code/files/mvc

$ vim index.html

Open a File Relative to the Current Working Directory

In Vim, just as in bash and other shells, we have the notion of a working

directory. When Vim is launched, it adopts the same working directory that

was active in the shell. We can confirm this by running the :pwd Ex command,

which (just as in bash) stands for “print working directory”:



The :edit {file} command can accept a filepath relative to the working directory.

If we wanted to open the lib/framework.js file, we could do so by running this


:edit lib/framework.js

Or we could open the app/controllers/Navigation.js file by running this:

:edit app/controllers/Navigation.js

We can use the tab key to autocomplete these filepaths (see Tip 32, on page

65, for more details). So if we wanted to open the Navigation.js file, we could

simply press :edit acN.

report erratum • discuss

Open a File by Its Filepath Using ‘:edit’

• 95

Open a File Relative to the Active File Directory

Suppose that we’re editing the app/controllers/Navigation.js file, and we decide that

we want to edit the Main.js file in the same directory. We could drill down to it

from our working directory, but that feels like unnecessary work. The file we

want to open is in the same directory as our active buffer. It would be ideal

if we could use the context of the active buffer as a reference point. Try this:

:edit %

The % symbol is a shorthand for the filepath of the active buffer (see

:h cmdline-special ). Pressing the key expands the filepath, revealing the

absolute path of the active buffer. That’s not quite what we want, but it’s

getting close. Now try this instead:

:edit %:h

The :h modifier removes the filename while preserving the rest of the path (see

:h ::h ). In our case, typing %:h is expanded to the full path of the current

file’s directory:

:edit app/controllers/

From there, we can type Main.js (or have the tab key autocomplete it for us),

and Vim will open the file. In total, we have to enter only the following


:edit %:h M

The %:h expansion is so useful that you might want to consider creating a

mapping for it. Check out Easy Expansion of the Active File Directory, on page

95, for a suggestion.

Easy Expansion of the Active File Directory

Try sourcing this line in your vimrc file:

cnoremap %%

getcmdtype() == ':' ? expand('%:h').'/' : '%%'

Now when we type %% on Vim’s : command-line prompt, it automatically expands to

the path of the active buffer, just as though we had typed %:h . Besides working

nicely with :edit, this can come in handy with other Ex commands such as :write, :saveas,

and :read.

For more ideas on how to use this mapping, see the Vimcasts episode on the :edit



report erratum • discuss

Chapter 7. Open Files and Save Them to Disk

• 96

Tip 42

Open a File by Its Filename Using ‘:find’

The :find command allows us to open a file by its name without having to provide

a fully qualified path. To exploit this feature, we first have to configure the ‘path’


We can always use the :edit command to open a file by providing its full path.

But what if we’re working on a project where the files are nested a few directories deep? Entering the full path every time we want to open a file can get

tiresome. That’s where the :find command comes in.


We’ll use the files/mvc directory to demonstrate. The source files are distributed

with this book. In the shell, we’ll launch Vim from the files/mvc directory:

$ cd code/files/mvc

$ vim index.html

Let’s see what happens if we attempt to use the :find command right now:

:find Main.js

E345: Can't find file "Main.js" in path

The error message tells us that no Main.js file can be found in the path. So let’s

do something about it.

Configure the ‘path’

The ‘path’ option allows us to specify a set of directories inside of which Vim

will search when the :find command is invoked (see :h 'path' ). In our case, we

want to make it easier to look up files in the app/controllers and app/views directories. We can add these to our path simply by running this:

:set path+=app/**

The ** wildcard matches all subdirectories beneath the app/ directory. We

discussed wildcards in Populate the Argument List, on page 81, but the

treatment of * and ** is slightly different in the context of the ‘path’ setting (see

:h file-searching ). The wildcards are handled by Vim rather than by the shell.

report erratum • discuss

Open a File by Its Filename Using ‘:find’

• 97

Smart Path Management with rails.vim

Tim Pope’s rails.vim plugin does some clever things to make navigating around a

Rails project easier.a The plugin automatically configures the ‘path’ setting to include

all the directories found in a conventional Rails project. This means that we can use

the :find command without having to worry about setting up the ‘path’.

But rails.vim doesn’t stop there. It also provides convenience commands, such as

:Rcontroller, :Rmodel, :Rview, and others. Each of these acts as a specialized version of the

:find command, scoping its search to the corresponding directory.


Use ‘:find’ to Look up Files by Name

Now that we’ve configured our ‘path’, we can open files in the directories we

specified by providing just their filename. For example, if we wanted to open

the app/controllers/Navigation.js file, we could enter this command:

:find Navigation.js

We can use the key to autocomplete filenames, so in fact we can get

what we want by typing as little as :find nav followed by the Enter key.

You might be wondering what happens if the specified filename is not unique.

Let’s find out. In our demo codebase, we have two files named Main.js: one is

in the app/controllers directory and the other is in app/views.

:find Main.js

If we type out the Main.js filename and then hit , Vim expands the entire

path of the first full match: ./app/controllers/Main.js. Press a second time,

and the next matching filepath takes its place, in this case ./app/views/Main.js.

When we press the Enter key, Vim will use the entire filepath if it has been

expanded or the first full match if no expansion has been performed.

You may observe slightly different tab-completion behavior if you have changed

the ‘wildmode’ setting from its default value of full. Refer to Tip 32, on page 65,

for more details.

report erratum • discuss

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

Tip 41. Open a File by Its Filepath Using ‘:edit’

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