3

I'm currently using superTab for completions in Vim. However, I'd like the completions to be more like bash. For example, if I'm typing

st

and the possible completions are

struct, string

I'd like it to be completed to str if I press tab, and ideally display a menu of possible completions.

Plugins are OK.

1
2

I think the following plugin does what you're asking for:
YouCompleteMe

From the description:

YouCompleteMe is a fast, as-you-type, fuzzy-search code completion engine for Vim. It has two completion engines: an identifier-based engine that works with every programming language and a semantic, Clang-based engine that provides semantic code completion for C/C++/Objective-C/Objective-C++ (from now on referred to as “the C-family languages”).

1

Putting set completeopt=menu,longest turns out to do the trick, mostly. Unfortunately entering some more text, then pressing tab again tries to do a full completion instead of giving the next longest common prefix.

2
  • I feel like that might be a supertab issue - adding completeopt+=longest makes it work more nicely for me using the native CTRL-N completion – JonnyRaa Nov 28 '17 at 15:17
  • Actually, I get what you mean... have posted an answer – JonnyRaa Dec 1 '17 at 16:52
0

Building on Alexander's answer, I have something like these lines in my .vimrc

"unix like completion - longest substring
set completeopt+=longest
"add command to retrigger longest substring
"Naive version... see below
inoremap <C-m> <C-e><C-n>

So the 'longest' bit does most of the work, however subsequent presses of <C-n> will just start to cycle through the options. If you press <C-e> that (e)nds autocompletion, triggering it again with <C-n> matches the next longest substring... adding a shortcut for that stops you having to press two keys....

You don't want to bind that to <C-n> as it will stop you cycling in the familiar manner

I chose M as it is right next to N and checking (:h insert-index) defaults to just being bound to same as <CR>.... because this is vim and nothing is ever easy it turns out that this means that vim can't actually distinguish between the two keypresses!

If you don't care about using <C-m> then just pick something else, otherwise read on...

Based on that other post I adapted the answer at the bottom and cleaned up the syntax a little (based on this)

I ended up with this:

"add command to retrigger longest substring
inoremap <expr> <C-M> pumvisible() ? "\<C-e><C-n>" : "\<C-m>"

pumvisible() returns a flag that says whether the pop-up menu is open. This stops us changing the behaviour of the enter key in insert mode when the popup menu isn't open.

Unfortunately I can't find any documentation for the ? and : and why we have to pass strings in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.