0

I'm on my MacBook using Vim, and let's say for simplicity's sake that I have files ~/some_file.py, ~/some_other_file.py, and ~/user.py open. On macOS, ~ expands to /Users/<username>.

So if I type :b user in Vim command line and then hit tab to expand, it goes through each of the files instead of going straight to ~/user.py.

Is there any way to prevent this behavior?

2 Answers 2

0

I can't reproduce your problem under linux (tildes are not resolved in my vim's completion list, so :b home gives me ~/home.py before ~/some_file.py), but...

Try typing :b user then complete with Shift+Tab. In that case, my vim (7.2.442 if that matters) completes with the last match, which is what you want.

3
  • You can't reproduce the problem because you probably open files in Vim right in the home directory. Try this: cd /tmp; vim ~/some_file.py ~/home.py, and you'll likely have the problem. At least I do in this case, so the behavior mentioned in the question is confirmed.
    – ib.
    Oct 13, 2010 at 2:49
  • @ib, you're absolutely right. Completing with Shift+Tab still works, though. Oct 13, 2010 at 7:33
  • Yes, I agree that Shift+Tab is the best life-hack in this situation.
    – ib.
    Oct 14, 2010 at 3:47
0

It is not possible to change Vim built-in buffer completion. The only thing I can suggest (besides opening these files already from the home directory) is to define your own version of the :b command with the desired completion. It could be something like this:

function! CustomBufferComplete(a, l, p)
    let buf_out = ''
    redir => buf_out
    silent buffers
    redir END

    let buf_list = map(split(buf_out, "\n"), 'substitute(v:val, ' .
    \   '''^.*"\%(\~[/\\]\)\?\([^"]\+\)".*$'', "\\1", "g")')
    return join(buf_list, "\n")
endfunction

command! -nargs=1 -complete=custom,CustomBufferComplete B b <args>

(Note that it cuts off the ~/ part of a path before returning the completion list.)

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.