0

Say I have a nnoremap and I want to pass the path of parent directory of current file to it, so the command can use this information. I have read this answer: How can I see the full path of the current file? but Still don't know how to do it?

I just found that % almost do the job, but it returns path to current file.

0

OK, after some try-and-error I resolved this problem. To get the path to parent dir:

%:p:h

Notice that the :p is needed to get the full path, without it you might end up with nothing, i.e. it's possible you cannot find the parent folder.

For example, my usage:

nnoremap <nowait><silent> <Tab>
                        \ :Fern %:h -drawer -width=36 -toggle -reveal=%<CR>

As you can see it is used directly inside the command, without any parentheses, single/double quotes, curly braces around it.

1
  • 1
    You can also use expand() when you need this in a context where it is not automatically expanded – D. Ben Knoble Apr 2 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.