Goal:
To achieve filename/path completion relative to current file directory using file name completion CTRL-X CTRL-F
.
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 CTRL-X CTRL-F
.
Attempt:
" 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 w:tmpCwd=w:baseCwd
.
:execute
to interpolate variables. This is likely a duplicate.:execute
in this questions context?execute 'lcd' w:baseCwd
for the command that goes back to the top directory...lcd w:baseCwd
withexecute 'lcd' w:baseCwd
. This works during first execution of function and successfully changes:pwd
to current file directory. During second execution it changes:pwd
back to the top directory but also throws error: E13: File exists (add ! to override). It keeps on throwing this error in subsequent executions.