Open file to line

3.4
(252)

You may already know about that you can open a file based on what’s under your cursor using gf, or ctrl-w f. I like to use gf and remember the handy mnemonic “ g o to f ile” and it’s one of the primary ways I navigate projects in Vim.

But lesser known is a handy variation on this using capital F. This variation will open the file under your cursor, but if it’s followed by a line number Vim will jump to that line. One place you might encounter this if you’re running tests in Vim with Dispatch.

Say you’re in a log file which contains file references:

spec/models/order_spec.rb:126
spec/controllers/admin/user_controller_spec.rb:74
app/models/product.rb:35

With your cursor sitting anywhere on a line containing the file name, use gF. You’ll be transported to the file and land exactly on the line referenced after the colon, saving you from having to navigate down to that line separately. Saving steps is saving time. Here’s a very short screencast to demonstrate:

How useful was this post?

Average rating 3.4 / 5. Vote count: 252

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

60 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

Leave a reply

Your email address will not be published. Required fields are marked *