ChatGPT解决这个技术问题 Extra ChatGPT

How do I make Vim do normal (Bash-like) tab completion for file names?

vim

When I'm opening a new file in Vim and I use tab completion, it completes the whole file name instead of doing the partial match like Bash does. Is there an option to make this file name tab completion work more like Bash?

Technically speaking, I'm pretty sure "bash" doesn't do any tab completion. It's readline that does that.
We are talking about customize the behavior of the ^X^F feature?

P
Peter Mortensen

I personally use

set wildmode=longest,list,full
set wildmenu

When you type the first tab hit, it will complete as much as possible. The second tab hit will provide a list. The third and subsequent tabs will cycle through completion options so you can complete the file without further keys.

Bash-like would be just

set wildmode=longest,list 

but the full is very handy.


I wish this were the default behavior in vim. I though there was no getting around to full tab completion until I saw this post.
@NehaKaranjkar I recommend maintaining a dotfiles archive to make it easy to deploy in new environments
Is there a way to start full only when there are few results? something like set wildmode=longest,5full,list. With lot results full is useless but very handy with few. It will be perfect with something like "show full only if completion fits on one line screen"
This used to be working in the past (gVim/ubuntu) but now it does not (macvim8.x/macOS). See example. Any ideas?
After tolerating the origin tab completion for 2 month, I think there must be a way to simplify it. Thank you.
C
Community

The closest behavior to Bash's completion should be

set wildmode=longest:full,full

With a few character typed, pressing tab once will give all the matches available in wildmenu. This is different to the answer by Michael which opens a quickfix-like window beneath the command-line.

Then you can keep typing the rest of the characters or press tab again to auto-complete with first match and circle around it.


I think I prefer this way.
Same! With this way, you can still press if you want to see the full list.
Thanks! This was the desired behavior. I clearly didn't understand the documentation, but after a second read, it is quite obvious: "Completion mode that is used for the character specified with 'wildchar'... Each part specifies what to do for each consecutive use of 'wildchar'. The first part specifies the behavior for the first use of 'wildchar', The second part for the second use, etc." :help wildmode
P
Peter Mortensen

Apart from the suggested wildmode/wildmenu, Vim also offers the option to show all possible completions by using Ctrl + D. This might be helpful for some users that stumble across this question when searching for different autocompletion options in Vim like I did.


P
Peter Mortensen

If you don't want to set the wildmenu, you can always press Ctrl + L when you want to open a file. Ctrl + L will complete the filename like Bash completion.


Ctrl-L does not act like bash autocomplete. It does not cycle through all possible options.
My bash does act like that. <Tab> completes the current string as far as is unambiguously possible (like CTRL-L in Vim) and <Tab><Tab> shows a list of possible matches (like CTRL-D in Vim).
P
Peter Mortensen

I'm assuming that you are using autocomplete in Vim via Ctrl + N to search through the current buffer. When you use this command, you get a list of solutions; simply repeat the command to go to the next item in the list. The same is true for all autocomplete commands. While they fill in the entire word, you can continue to move through the list until you arrive at the one you wish to use.

This may be a more useful command: Ctrl + P. The only difference is that Ctrl + P searches backwards in the buffer while Ctrl + N searches forwards... Realistically, they will both provide a list with the same elements, and they may just appear in a different order.


A
André Willik Valenti

set wildmode=longest:full gives you a Bash-like completion with:

suggestions in a single line

Tab completing only what is certain

Right/Ctrl-n | Left/Ctrl-p to select suggestions.

https://i.stack.imgur.com/upRoT.gif

From the help:

If you prefer the <Left> and <Right> keys to move the cursor instead
of selecting a different match, use this:
      :cnoremap <Left> <Space><BS><Left>
      :cnoremap <Right> <Space><BS><Right>

P
Peter Mortensen

Try using :set wildmenu. Apart from that, I'm not sure what exactly you're trying.

Oh, yeah, and maybe try this link: link