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 57. Jump to the Filename Under the Cursor

Tip 57. Jump to the Filename Under the Cursor

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

Jump to the Filename Under the Cursor



• 135



In the shell, we’ll start by changing to the jumps directory and then launching

Vim. For this demonstration, I recommend using the -u NONE -N flags to ensure

that Vim starts up without loading any plugins:









$ cd code/jumps

$ vim -u NONE -N practical_vim.rb



The practical_vim.rb file does nothing more than load the contents of the core.rb

and more.rb files:

jumps/practical_vim.rb

require 'practical_vim/core'

require 'practical_vim/more'



Wouldn’t it be useful if we could quickly inspect the contents of the file

specified by the require directive? That’s what Vim’s gf command is for. Think

of it as go to file (:h gf ).

Let’s try it out. We’ll start by placing our cursor somewhere inside the 'practical_vim/core' string (for example, pressing fp would get us there quickly). If we

try using the gf command now, we get this error: “E447: Can’t find file

‘practical_vim/core’ in path.”

Vim tries to open a file called practical_vim/core and reports that it doesn’t exist,

but there is a file called practical_vim/core.rb (note the file extension). Somehow

we need to instruct Vim to modify the filepath under the cursor by appending

the .rb file extension before attempting to open it. We can do this with the

‘suffixesadd’ option.



Specify a File Extension

The ‘suffixesadd’ option allows us to specify one or more file extensions, which

Vim will attempt to use when looking up a filename with the gf command

(:h 'suffixesadd' ). We can set it up by running this command:







:set suffixesadd+=.rb



Now when we use the gf command, Vim jumps directly to the filepath under

the cursor. Try using it to open more.rb. In that file, you’ll find a couple of

other require declarations. Pick one, and open it up using the gf command.

Each time we use the gf command, Vim adds a record to the jump list, so

we can always go back to where we came from using the command (see

Tip 55, on page 131). In this case, pressing the first time would take us

back to more.rb, and pressing it a second time would take us back to practical_vim.rb.



www.it-ebooks.info



report erratum • discuss



Chapter 9. Navigate Between Files with Jumps



• 136



Specify the Directories to Look Inside

In this example, each of the files referenced with the require statement was

located relative to the working directory. But what if we referenced functionality that was provided by a third-party library, such as a rubygem?

That’s where the ‘path’ option comes in (:h 'path' ). We can configure this to

reference a comma-separated list of directories. When we use the gf command,

Vim checks each of the directories listed in ‘path’ to see if it contains a filename

that matches the text under the cursor. The ‘path’ setting is also used by the

:find command, which we covered in Tip 42, on page 96.

We can inspect the value of the path by running this command:









:set path?

path=.,/usr/include,,



In this context, the . stands for the directory of the current file, whereas the

empty string (delimited by two adjacent commas) stands for the working

directory. The default settings work fine for this simple example, but for a

larger project we would want to configure the ‘path’ setting to include a few

more directories.

For example, it would be useful if the ‘path’ included the directories for all

rubygems used in a Ruby project. Then we could use the gf command to

open up the modules referenced by any require statements. For an automated

solution, check out Tim Pope’s bundler.vim plugin,1 which uses the project

Gemfile to populate the ‘path’ setting.



Discussion

In the setup for this tip, I recommended launching Vim with plugins disabled.

That’s because Vim is usually distributed with a Ruby file-type plugin, which

handles the setup of ‘suffixesadd’ and ‘path’ options for us. If you do a lot of work

with Ruby, I recommend getting the latest version of the file-type plugin from

github because it’s actively maintained.2

The ‘suffixesadd’ and ‘path’ options can be set locally for each buffer, so they can

be configured in different ways for different file types. Vim is distributed with

file-type plugins for many other languages besides Ruby, so in practice you

won’t often have to set these options yourself. Even so, it’s worth understanding how the gf command works. It makes each filepath in our document

behave like a hyperlink, which makes it easier to navigate through a codebase.

1.

2.



https://github.com/tpope/vim-bundler

https://github.com/vim-ruby/vim-ruby



www.it-ebooks.info



report erratum • discuss



Snap Between Files Using Global Marks



• 137



The command has a similar role. It also requires a bit of setup (as discussed in Tip 102, on page 252), but when it’s correctly configured, it allows

us to jump from any method invocation directly to the place where it was

defined. Skip ahead to Tip 103, on page 254, for a demonstration.

While the jump list and change list are like breadcrumb trails that allow us

to retrace our steps, the gf and commands provide wormholes that

transport us from one part of our codebase to another.



Tip 58



Snap Between Files Using Global Marks

A global mark is a kind of bookmark that allows us to jump between files.

Marks can be especially useful for snapping back to a file after exploring a

codebase.

The m{letter} command allows us create a mark at the current cursor position

(:h m ). Lowercase letters create marks that are local to a buffer, whereas

uppercase letters create global marks. Having set a mark, we can snap our

cursor back to it with the `{letter} command (:h ` ).

Try this: open up your vimrc file and press mV to set a global mark (mnemonic:

V for vimrc). Switch to another file and then press `V , and you should snap

back to the global mark you set in the vimrc file. By default, global marks are

persisted between editing sessions (although this behavior can be configured;

see :h 'viminfo' ). Now you can always open up your vimrc file with two

keystrokes—that is, unless you set the global V mark to another location.



Set a Global Mark Before Going Code Diving

Global marks can be especially useful when we need to browse through a set

of files and then quickly snap back to where we started. Suppose that we’re

working on some code, and we want to find all occurrences of a method called

fooBar() in our codebase. We could use the :vimgrep command (covered in Tip

110, on page 273):







:vimgrep /fooBar/ **



By default, :vimgrep jumps directly to the first match that it finds, which could

mean switching to another file. At this point, we can use the command

to get back to where we were prior to running :vimgrep.



www.it-ebooks.info



report erratum • discuss



Chapter 9. Navigate Between Files with Jumps



• 138



Let’s say that our codebase contains dozens of matches for the pattern fooBar.

For each match :vimgrep finds, it creates a record in the quickfix list. Now

suppose that we spend a minute or two traversing that list until eventually

we find what we are looking for. Now we want to get back to where we were

before we ran the :vimgrep command. How do we do it?

We could get there using the command to reverse through the jump

list, but it might take a while. This is a scenario where a global mark would

come in handy. If we had run the mM command before invoking :vimgrep, then

we could snap back in one move with the `M command.

Advice is rarely welcome when it goes “You should have started by doing X.”

Global marks are only useful if we have the forethought to set them up correctly in advance. With practice, you’ll learn to recognize the scenarios where

it would be useful to set a global mark.

Try to get into a habit of setting a global mark before using any commands

that interact with the quickfix list, such as :grep, :vimgrep, and :make. The same

goes for the commands that interact with the buffer and argument lists, such

as :args {arglist} and :argdo (see Tip 37, on page 80).

Remember, you can set up to twenty-six global marks, which is more than

you’ll ever need. Use them liberally; set a global mark any time you see

something that you might want to snap back to later.



www.it-ebooks.info



report erratum • discuss



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

Tip 57. Jump to the Filename Under the Cursor

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

×