Create Autocompletion snippets for Custom Code

72 views
Skip to first unread message

Amit Christian

unread,
Aug 23, 2016, 12:38:19 PM8/23/16
to vim_use
Is it possible to create your own autocompletion snippets using Vim functionalities or plugin? I have several custom code that I would like to create snippet for. I am using GVim on Windows. Most of existing autocomplete plugins like NeoComplete or YouCompleteMe etc do not work well on Windows. I am looking for something Vim native to do this.

Any help, suggestions are appreciated.

Thanks.

Tumbler Terrall

unread,
Aug 23, 2016, 12:45:30 PM8/23/16
to vim_use

Yes it is! Have a look at `:h complete` and `:h compl-functions`. They should get you pointed in the right direction. :)

Amit Christian

unread,
Aug 23, 2016, 3:23:51 PM8/23/16
to vim_use

Thanks Tumbler.

I started working on it. The Help had a example that I could work with.

inoremap <F5> <C-R>=ListMonths()<CR>

func! ListMonths()
call complete(col('.'),
['January
New line', 'February', 'March',
\ 'April', 'May', 'June', 'July', 'August', 'September',
\ 'October', 'November', 'December'])
return ''
endfunc

Is it possible to have completion triggers snippet with multiple lines? For example,

F5 > January will put

First Month.
January.

Something like that. Is it possible?

Tumbler Terrall

unread,
Aug 23, 2016, 3:59:49 PM8/23/16
to vim_use
> Is it possible to have completion triggers snippet with
> multiple lines?

You're asking if a completion suggestion can be multiple
lines/have new-lines in them? I'm not the definitive
source on this, but I don't think so. I tried all line
breaking methods I could think of and none of them worked.
Unfortunately for you, I think it's designed as line by
line kind of a thing, sorry.

Regards,
~Tumbler

Luc Hermitte

unread,
Aug 23, 2016, 5:51:11 PM8/23/16
to vim use
Hi

> Is it possible to create your own autocompletion snippets using Vim
> functionalities or plugin? I have several custom code that I would
> like to create snippet for. I am using GVim on Windows. Most of
> existing autocomplete plugins like NeoComplete or YouCompleteMe etc
> do not work well on Windows. I am looking for something Vim native
> to do this.

There exist several template expander plugins that work on Window. mu-template is one of them. It's indeed 100% native (i.e. written only in viml).
-> https://github.com/LucHermitte/mu-template

If you want a smart completion, its internal completion function is available in lh-vim-lib
https://github.com/LucHermitte/lh-vim-lib/blob/master/autoload/lh/icomplete.vim#L136
(it has been inspired from YCM equivalent feature ; see http://vi.stackexchange.com/questions/5820/dynamic-completion regarding its birth)

You'll find examples of use within mu-template code base and in the accompanying documentation.

HTH,

--
Luc Hermitte

Amit Christian

unread,
Aug 24, 2016, 2:56:00 PM8/24/16
to vim_use
Thanks Luc.

I am looking into that. I am beginner/intermediate level of Vim user. And I find it really hard to add my own custom snippet in the mu-template. Can you give me some ideas of how to add simple snippets.

For example
-----------

I would like to add the following for the trigger `openr`.

OPENR, unit, filename, /GET_LUN

cursor here

CLOSE, unit
FREE_LUN, unit

Luc Hermitte

unread,
Aug 25, 2016, 7:22:06 AM8/25/16
to vim use
Hi,

> I am looking into that. I am beginner/intermediate level of Vim user.
> And I find it really hard to add my own custom snippet in the
> mu-template. Can you give me some ideas of how to add simple
> snippets.
>
> For example
> -----------
>
> I would like to add the following for the trigger `openr`.
>
> OPENR, unit, filename, /GET_LUN
>
> cursor here
>
> CLOSE, unit
> FREE_LUN, unit

Indeed, I see I shall add a "how do i create a new snippet/template?" section in the help.

I don't know the filetype your snippet is for, let's say it's "foobar".

Create a "template/foobar" directory under the vimfiles/ directory in your $HOME (%HOMEDRIVE%%HOMEPATH% in windows).

In your $HOME/vimfiles/template/foobar, create a file named openr.template, it should get filed with default mu-template boilerplate settings.

Then, add what you want in your template file. If you keep the default

VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'

Your can define placeholders with "<++>", or "<+placeholdername+>". It's even possible to accept parameters that can be injected from other snippets, or programmatically. (see :h s:Params()).

So, I guess, your template would look like:

VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'
OPENR, <+unit+>, <+filename+>, /GET_LUN

<+cursor here+>

CLOSE, <+unit+>
FREE_LUN, <+unit+>



NB: I've simplified the explanation regarding the exact directory where you could put your new template files. see :h MuT-paths-override

HTH,

--
Luc Hermitte

Amit Christian

unread,
Aug 26, 2016, 10:08:19 AM8/26/16
to vim_use
Luc,

Thank you. I installed the plugin. But I can't get it to work.

I have attached the error. I have followed the direction and got all the plugins (as shown below).

git clone g...@github.com:LucHermitte/lh-vim-lib.git
git clone g...@github.com:LucHermitte/lh-tags.git
git clone g...@github.com:LucHermitte/lh-dev.git
git clone g...@github.com:LucHermitte/lh-brackets.git
git clone g...@github.com:LucHermitte/searchInRuntime.git
git clone g...@github.com:LucHermitte/mu-template.git

Thanks.

2016-08-26_Message-000060.png

Amit Christian

unread,
Aug 29, 2016, 9:34:18 AM8/29/16
to vim_use
Luc,

Thanks. I updated it and it is working. I will test and will let you know.

Thanks again.

Reply all
Reply to author
Forward
0 new messages