When I write markdown and want to insert an image, i_CTRL-X_CTRL-F comes in handy. I know that I can filter by typing a few characters that precede the filename, but actually what I want is either type *.jpg and see the completion list filter on jpegs or have an option to filter the completion list once it is open on *.jpg

How can I improve my local file selection workflow, preferably without a plugin?

  • Why restrict your options with "preferably without a plugin". Pretty sure there are plugins that expand ctrl-x completion to use wildcards in the way you describe.
    – B Layer
    Commented Sep 17, 2019 at 16:15
  • That's why I wrote preferably. I get a long way without plugins and use them where appropriate. When there is a Vim native solution I prefere it over many tiny plugins cumbersome to keep current (using no plug-in manager, only Vim8 packages)
    – JohnDoe
    Commented Sep 17, 2019 at 20:07
  • Fair enough but if it's "prefer native but will use plugin if need be" you don't really have to mention that as that's pretty much everyone's stance...never seen anyone say "preferably with a plugin". ;)
    – B Layer
    Commented Sep 17, 2019 at 20:20

1 Answer 1


The relevant example with explanations can be found under :h complete()

function! MyCompleteFileName()
    " match a (potential) wildcard preceding cursor position
    " note: \f is a filename character, see :h 'isfname'
    let l:pattern = matchstr(strpart(getline('.'), 0, col('.') - 1), '\v(\f|\*|\?)*$')
    " set the matches
    call complete(col('.') - len(l:pattern), getcompletion(l:pattern, "file"))
    " must return an empty string to show the menu
    return ''

" rebind <C-X><C-F> to invoke our custom function
inoremap <C-X><C-F> <C-R>=MyCompleteFileName()<CR>
  • That fails with a lot of error messages: (can't copy from :messages): E15: invalid expression: \v(\f|\|\?)*$
    – JohnDoe
    Commented Sep 18, 2019 at 10:54
  • @JohnDoe Somehow, I've lost an opening quote while copying the code. That's all.
    – Matt
    Commented Sep 18, 2019 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.