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?

ib.
  • 27,830
  • 11
  • 80
  • 100
Jason Baker
  • 192,085
  • 135
  • 376
  • 510

2 Answers2

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.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 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 '10 at 02:49
  • @ib, you're absolutely right. Completing with Shift+Tab still works, though. – Frédéric Hamidi Oct 13 '10 at 07:33
  • Yes, I agree that `Shift+Tab` is the best life-hack in this situation. – ib. Oct 14 '10 at 03: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.)

ib.
  • 27,830
  • 11
  • 80
  • 100