3

I often complete file paths in Vim using Ctrl-x Ctrl-f in insert mode, but there is one annoyance: the tilde character does not expand. This is fine for things like bash, but for Python (which does not understand ~/Data/data.xml) it is suboptimal.

For example:

~/Dat completes to ~/Data/ whereas I would prefer the full path /home/user/Data/. (As a side note: Vim already expands shell variables correctly, so that $HOME/Dat completes to /home/user/Data.)

Is there a way to get file path completion to expand the tilde? If not natively, is there a plugin that does so?

  • 2
    If you write it like this ~user/foo/bar, the tilde will be correctly expanded – Christian Brabandt Jan 12 '16 at 21:51
4

If you press Ctrl+xCtrl+f immediately after typing the ~, i.e. before typing the trailing slash, it is expanded to /home/user/.

  • Interesting; I'd never noticed that before! Still, I would prefer it expand in other cases too if possible as I habitually type a few characters before hitting the expansion keys. – Alex Jan 12 '16 at 20:50
  • 1
    Vimple (github.com/dahu/vimple) has a filename completion option that honours 'path' setting, which has this side effect. It needs quite recent vim and doesn't work quite the same as vim's default, think of it more as an example. – Sakari Cajanus Jan 12 '16 at 21:34
2

How about using an input abbreviation? E.g. using iabbrevv ~~ /home/duck, when you type ~~/ it will be expanded to /home/duck/ immediately and you can continue as normal.

Or maybe just a mapping:

inoremap <C-x><C-f> <C-[>viW:s/\~/\/home\/duck/<CR>Ea<C-x><C-f>

This will simply substitute the tilde of the WORD you're typing on by your homedir. It might be improved by using a mark to save your cursor position, but I kinda guess you're fine with just continuing to type at the end of the WORD.

  • I like the idea of an abbreviation, I'll have to play around with that. – Alex Jan 14 '16 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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