I have a function in Vim script that returns a string.
" Return first directory with ".git" folder, if none is found
" return the current directory
function! s:project_root()
for vcs in ['.git']
let dir = finddir(vcs.'/..', expand('%:p:h').';')
if !empty(dir)
return dir
endif
endfor
" If there is no root dir use the current dir
return expand('%:p:h')
endfunction
I would like to use this function in a key mapping as the argument to a command called Files
. So something like this
nnoremap <leader>f :Files<Space><insert result of function call here><CR>
I tried doing:
execute 'nnoremap <leader>f :Files<Space>'.s:project_root()
but I believe this only calls the function once (when the .vimrc is loaded), when I need the function to be called everytime the keybinding is pressed.
nnoremap <leader>f :Files s:project_root()<CR>
should work. I can't get what a problem you are facing.s:project_root()
as the directory name itself, instead of calling it.