Bash-like code completion in vim?

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.

Alexander Duchene

Posted 2013-03-29T04:42:11.190

Reputation: 153

1

Please don't post the identical question on multiple sites: http://stackoverflow.com/questions/15697498/bash-like-code-completion-in-vim

– Ingo Karkat – 2013-03-29T06:37:00.167

Answers

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

Josh Whittington

Posted 2013-03-29T04:42:11.190

Reputation: 280

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.

Alexander Duchene

Posted 2013-03-29T04:42:11.190

Reputation: 153

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 – 2017-11-28T15:17:19.157

Actually, I get what you mean... have posted an answer – JonnyRaa – 2017-12-01T16:52:01.470

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.

JonnyRaa

Posted 2013-03-29T04:42:11.190

Reputation: 183