For example, I want to write


and complete the path with i_CTRL-X_CTRL-F so that it becomes:


But what I get is:


One workaround I've tried is using ~ instead of $HOME, but they aren't equivalent, e.g. "echo "$HOME" is not the same as echo "~", and ~ doesn't help with other environment variables.

  • 1
    Note that the ^X^F completion doesn't use bash, it's built in to Vim itself. – Martin Tournoij Oct 7 '15 at 15:19
  • 2
    From a quick reading of the source, it seems that this is not possible. In edit.c, ins_compl_get_exp() is called, which expands all environment variables (as well as patterns like %:h). On the next line, the tilde seems to be actually re-added with tilde_replace()... You could override the ^X^F completion with your own version though... – Martin Tournoij Oct 7 '15 at 15:28
  • I see what you mean: github.com/vim/vim/blob/master/src/edit.c#L4398 – Nathaniel M. Beaver Oct 7 '15 at 16:44
  • AFAIK Shougo's neocomplete will do what you want, by default, using its file completion source. – VanLaser Oct 7 '15 at 20:50
  • Permalink for previous comment: github.com/vim/vim/blob/… – Nathaniel M. Beaver May 20 '20 at 21:00

This is probably impossible without any plugins, as mentioned on the comments.

But you wouldn't have to start from the scratch if you want to implement such functionality using VimL, you could use the BufNameComplete plugin as a starting point (note that it uses the CompleteHelper : Generic functions to support custom insert mode completions plugin, which probably isn't necessary for your problem).

You could use a regex to identify any environment variables before the expansion and then apply substitute(result, expanded_env_var, original_env_var, '') in order to restore the environment variables.

(actually this works if you want to check only for the first environment variable on the input; if you want to preserve all of them,you would need to build a dictionary with all the variables and the respective expansions, and then iterate on it to replace each expansion with the original variable)

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.