开发者

How do I prevent Vim from expanding ‘~’ during completion?

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 instea开发者_C百科d of going straight to ~/user.py.

Is there any way to prevent this behavior?


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.


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜