3

Vim Relative File Autocomplete

 1 year ago
source link: https://gosukiwi.github.io/vim/2022/10/26/vim-relative-file-autocomplete.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Vim Relative File Autocomplete

Oct 26, 2022

Vim’s file autocomplete (<C-x><C-f>) works only for absolute paths (using the current working directory) as root.

But in most languages, you want to use relative imports, and file autocomplete doesn’t work there. For that, I created the following <C-x><C-x><C-f> mapping:

function! s:EnableRelativeAutocomplete() abort
  let b:relative_autocomplete_cleanup_pending = 1
  lcd %:p:h
endfunction

function! s:DisableRelativeAutocomplete() abort
  if exists('b:relative_autocomplete_cleanup_pending') && b:relative_autocomplete_cleanup_pending
    lcd -
    let b:relative_autocomplete_cleanup_pending = 0
  endif
endfunction

inoremap <C-x><C-x><C-f> <C-o>:call <SID>EnableRelativeAutocomplete()<CR><C-x><C-f>

augroup relative_file_autocomplete
  autocmd!
  autocmd InsertLeave * call s:DisableRelativeAutocomplete()
augroup END

You can simply copy-paste that into your .vimrc. With that in place, you can use relative file autocomplete by using <C-x><C-x><C-f>. It will work just like <C-x><C-f> does.

It does the trick by changing the current working directory to your current file’s directory when using that mapping, and changing back to the previous directory when you leave insert mode.

Hope you find it as useful as I do!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK