views:

60

answers:

2

I'm on my macbook using vim, and let's say for simplicity's sake that I have ~/some_file.py, ~/some_other_file.py, and ~/user.py open. On the mac, ~ expands to /Users/<username>. So if I type in :b user 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?

A: 

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
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
@ib, you're absolutely right. Completing with Shift+Tab still works, though.
Frédéric Hamidi
Yes, I agree that `Shift+Tab` is the best life-hack in this situation.
ib
A: 

As it seems that it's not possible to change Vim built-in buffer completion, the only thing I can suggest (besides opening these files already being in the home directory) is to define your own :b command with desirable 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 completion list.)

ib