0

I got the following hotkey mapping:

nnoremap <leader>f Unite file -path=~/Workspace

This works great, however, I want to make it so that path equals the current folder I'm in (which will be seperate from working directory).

Anyone know how I can make this happen? :S

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
Robin Heggelund Hansen
  • 4,906
  • 6
  • 37
  • 54

3 Answers3

4

You can use the expand() function to use %:p:h in places where a file name is not expected (these expansions are for file-name arguments, not others like what it appears to happen with your command)

:echo expand('%:p:h')

But you can't map that directly. It is a command that needs to be built "on-the-fly", so you can use :execute to build and execute an evaluated expression:

nnoremap <leader>f :exec "Unite file -path=" . expand('%:p:h')
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • 1
    ⚠️ @ RobinHeggelundHansen Please check the other answers for more appropriate solutions as well, I'll keep mine just because it is "Vim generic" in case you, or a future viewer, needs to know this technique. – sidyll Nov 23 '15 at 22:12
3

How about using:

:UniteWithBufferDir file 

or

:UniteWithCurrentDir file

(depending on what you want)

VanLaser
  • 1,144
  • 6
  • 8
  • 1
    I don't understand about _Unite_ at all, and gave a generic answer. But this one seems to be the exact proper solution! – sidyll Nov 23 '15 at 22:05
2

Unite allows dynamic argument by using backtick, as documented in the Unite help doc:

You don't have to use |:execute| for dynamic arguments.
You can use evaluation cmdline by ``.
Note: In the evaluation, The special characters(spaces,  "\" and ":")
are escaped automatically.
>
    :Unite -buffer-name=search%`bufnr('%')` line:forward:wrap<CR>

So in your case, the mapping will be:

:nnoremap <leader>f :Unite file -path=`expand('%:p:h')`
DJ.
  • 6,664
  • 1
  • 33
  • 48