Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Popup Menu in VIM

Tags:

vim

Tab completion with a popup menu in vim works pretty well with the right configuration. http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE

I have a small collection of code generator and code manipulating programs which I use in vim. The procedure is:

1. initiate visual mode
2. highlight text
3. :'<,'>!hashify

I would like to harness vim popup menu to offer a selection of actions.

The new procedure would be:

1. initiate visual mode
2. highlight text
3. <Tab> -- select transform option from menu

Is there a vimscript interface which could be used for this?

like image 988
ddoxey Avatar asked Nov 24 '12 01:11

ddoxey


Video Answer


2 Answers

The insert mode completion popup can be used to insert a choice of text fragments. There are two ways to implement it, see :help complete-functions and :help complete(). If your code generator returns single-line (and not too long) text fragments to insert, you could invoke the generator via system(...) and then feed the returned values to the completion function.

On the other hand, if the menu choices do not directly correspond to inserted text, but are tactical choices or actions, most plugins present a selection menu like this, styled like built-in menus (e.g. from :ilist):

:echohl Title
:echo 'Code fragments:'
:echohl None
:echo '1. foo'
:echo '2. bar'
:let choice = nr2str(getchar())
:if choice == 1 ...

Then, insert the text corresponding to the choice via :normal! iText, or setline().

As you appear to need the completion from visual mode, you can first capture the selected text by starting your mapping with y.

like image 172
Ingo Karkat Avatar answered Sep 21 '22 12:09

Ingo Karkat


This plugin allows to create popups like that for Vim:

enter image description here

like image 38
adelarsq Avatar answered Sep 21 '22 12:09

adelarsq