Let's suppose I have the following environment variable

1| export  my__files=$HOME/Documents/MyFiles

Now let's suppose I am editing the file main.txt (see content below)

1| foo
2| bar
3| $my__files/█

If I press <C-x><C-f> (complete filename) while in insert mode in the current cursor position, it will expand the environment variable and start listing the files and directories present within that specific directory

1| foo
2| bar
3| /home/mario/Documents/MyFiles/DCBooks/█

My question is: When using file completion in front an environment variable is it possible to make vim not to expand it and just start listing files within the directory that is represented by the environment variable?

  • I’m not sure this happens for me in regular vim. Can you try with vim -u NONE? I’m pretty sure the usual mapping is <C-x><C-f> – D. Ben Knoble Nov 1 '19 at 2:01
  • I'm not stating that the usual mapping <C-x><C-f> doesn't work, I'm just stating that the environment variable expands to its literal value when pressing that combination of keys, but I don't want this behavior to occur. I don't want the environment variable to be expanded, that's why I was wondering if there is an option that disable that behavior. Note that <C-x><C-f> lists all unhidden files within a directory, which I want to use, but I repeat, I don't want the environment variable to be replaced with its literal value. – gfe Nov 1 '19 at 3:01
  • and I’m saying I’m not sure I’ve ever seen that happen, and that it could perhaps be from a plugin. The fact that the keys in the question are different from the usual strengthen my guess, but it’s a shot in the dark. That’s why I asked if this happened for you without a vimrc or with a minimal vimrc. – D. Ben Knoble Nov 1 '19 at 4:42
  • 1
    Any "file finding" function needs to expand an envvar, so you can't do with standard options. You can write your own custom completion function like this: build list of expanded names, and then shrink it back before feeding to complete(). See this answer for example of custom complete. – Matt Nov 1 '19 at 7:14
  • I believe that is how file expansion works. Vim will always exapnd environment variables. There is a todo item about it. – Christian Brabandt Nov 1 '19 at 7:30

Your Answer

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

Browse other questions tagged or ask your own question.