2

I wonder how to open a file from a directory under the cursor.

I have managed to do it for simple directories, but it requires they are one single word and are located in the current working directory: In commandline mode, with :e and then <ctrl+r> <ctrl+w> for pasting the word under cursor, and then using tab for example for autocompletion, or <Enter> for opening the :Explore view of it.

However I have no idea for more complex directories.

Is there a solution ?

Stephane Rolland
  • 1,857
  • 2
  • 13
  • 23
  • I'm not sure what you mean by "complex directories", but <c-r><c-a> will expand the WORD (where <c-r><c-w> expands a 'word'). Also, gf will open netrw if the file under the cursor is a directory. Of course, that only works if the file is in the path (:h path). – Biggybi May 28 '20 at 12:35
  • 1
    Both of your suggestions answer my point. <c-r><c-a> is great. And gf is just exactly what I was searching for. – Stephane Rolland May 28 '20 at 12:56

2 Answers2

5

Biggybi mentions gf in the comments, and it is usually the best way to go. It takes you to the file (in your path) under the cursor. This should work for directories, too.

Alternately, you could use :edit <cfile> or alternatives, as explained here. (I believe also :edit <C-r><C-f>.)

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
2

The simple way to open a file under cursor is gf (if it's in your path). When the file is a directory, this will open netrw (the builtin file explorer) for that directory.

You can also use :wincmd f for the same result.

Thus, you can set a mapping to open the file in a vertical split for example:

nnoremap g :vertical wincmd f

This can be done with <c-w><c-f>.

A more useful one could be to open the folder in a vertical split on the far-left hand side:

nnoremap g<c-f> :Lexplore <cfile><cr>

From command line, you can use <c-r><c-a> to expand the WORD. A WORD is a word delimited by white spaces, as opposed to word which is delimited by -, _, and other special characters.

Edit: <cfile>, as mentionned by D. Ben Knoble, is way better.

Biggybi
  • 2,740
  • 9
  • 29