To achieve filename/path completion relative to current file directory using file name completion
I open all my files contained in a base directory such that
:pwd in some_file outputs
/some/path/to/base_directory. This lets me search for other files relative to base_directory, that I want.
The problem with using
CTRL-X CTRL-F is that by default it prompts for filename/directory relative to base_directory. So, if one has to use a path name that is relative to current file directory, something like
../../../some_file pathname, one is out of luck, as this path name is of some_file in three directory level up from base_directory.
I am trying to write a mapping that can switch between base_directory and current file directory to make efficient use of
" temporarily switch to current file directory function! SwitchCwd() " on first execution of function from a window if exists("w:baseCwd")==0 " store the base directory path in w:baseCwd let w:baseCwd=getcwd() let w:tmpCwd=expand("%:p:h") " when this mapping is run first time from a window, switch to current file directory lcd %:p:h elseif w:tmpCwd==expand("%:p:h") echo w:baseCwd " on subsequent executions of this function from same window w:tmpCwd=w:baseCwd " switch to base directory lcd w:baseCwd else echo "inside else" lcd %:p:h endif endfunction "Path prompt mapping nnoremap <leader>cd :call SwitchCwd()<CR>
When I run this mapping with
<leader>cd for the first time, vim successfully switches to current file directory.
Any subsequent command
<leader>cd results in error:
Error detected while processing function SwitchCwd: line 13: E13: File exists (add ! to override) line 14: E344: Can't find directory "w:baseCwd" in cdpath E472: Command failed
echo w:baseCwd in
elseif outputs the correct absolute path of base_directory, but throws an error in assignment