Custom Popup Menu in VIM - 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?

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.

This plugin allows to create popups like that for Vim:

Related

How to efficiently copy code from editor to search field in VS Code using vscodevim?

This is currently how I copy code from an editor to a search field in VS code using vscodevim.
Select text in editor somehow
Right click to open up the contextual menu (since pressing Ctrl+C does not seem to work on Ubuntu, even when in input mode, and 'p' does not work in the search field) and click copy
Press Ctrl+Shift+F to open the search field
Press Ctrl+V
I'm pretty sure this is not how copying from an editor to search field is intended to work. It it the steps 1 and 2 I would like to change to something better.
What is a more efficient and vim-like sequence?
If you want to search for the word under the cursor
Ctrl-F will do the trick.
Or you can use Vim's * command, which effectively does the same, but jumps to the next occurrence right away by default.
Otherwise
If you need to use the search field for whatever reason, then the standard Vim way to copy stuff to the clipboard works, so you can yank into the * or + registers. The steps will then be:
Select text
"+y (you can create a shortcut for this combination if you want)
Ctrl-Shift-F, Ctrl-V
See also: How to make vim paste from (and copy to) system's clipboard?.
Having said that, the more obvious approach might be to use Vim's built-in search features, so after selecting the text, the remaining steps would be y: (yanking selection to the default register and opening the command-line) then / or ? (search forward or backward), then <C-v> (pasting the yanked selection to the command-line - this works only in the VSCode plugin, while in Vim you should use <C-r>").
I'm new to VIM so there might be a better way using VIM commands but for now this works pretty well.
Go into insert mode with i than select the word you would like to search for (I'm using the mouse) and than just press ctrl + f. Your search window will open as usual containing the selected word in it.

vimscript: add functionality to mouse click with key mapping

I use vim in xterms on Arch linux. Wanting to automatically set marks for subsequent command ranges I wrote this mapping
map <LeftMouse> mp:let g:oc=g:nc<cr>:let g:nc=getpos('.')<cr>:call setpos("'o", g:oc)<cr>:call cursor(g:nc[1], g:nc[2])<cr>
but now the mouse no longer places the cursor at the clicked location. How can I keep standard mouse function, and add to it rather than replacing it?
It seems its not possible to modify LeftMouse, but you can achieve the effect of it using LeftRelease. So my mapping does what I want as
nmap <LeftRelease> mp:let g:oc=g:nc<cr>:let g:nc=getpos('.')<cr>:call setpos("'o", g:oc)<cr>
and the standard function of is unchanged.
You can :set mouse=a for using visual select mode. (This is not what you wanted but it is a trick).
Then, click on text, you will see that it is getting selected. Then, you can release it.
A mapping like
:map gv ma
will set the last recently selected text as mark a.
Other way:
Instead of :set mouse=a, you can press v and then select a letter or a word, depending upon your convenience and then ma for marking it as mark a.
A short mapping for it will be
:map vly ma

Autocomplete javascript/DOM API in vim?

I always make typos when typing the long function names like ctx.createRadialGradient. It will be great if I can autocomplete them.
Is there any plugin I can use out of the box?
If not, how to add new custom keywords for vim's autocompletion?
In insert mode exists command Ctrl-XCtrl-P to complete with previous matches. So if you have:
ctx.createRadialGradient
ctx.createGradient
ctx.create
and begin to write ctx.cr, press Ctrl-XCtrl-P and those strings will appear in a context window, you can navigate throught them with arrow keys or with Ctrl-N for Next or Ctrl-P for Previous.

edit gvim mouse buttons

I have a two part question regarding mouse clicks on gvim.
1.) I would like to change my gvim double left mouse click into something similar to the windows environment (ie Notepad, Microsoft Word), wherein it: highlights the current word, and replaces it with whatever I type. This is akin to pressing 'cw' for changeword in vim.
2.) I would like my single mouse click to place a cursor upon the selected text and enter insert mode. Again like how a mouse normally behaves in Windows.
1) nmap <2-LeftMouse> ciw
You could use viw here, which will visually select the current word, but then you will still have to press c to replace it.
2) nmap <LeftMouse> <Leftmouse>i
Or replace the i with an a if you would prefer to append, rather than insert.
You can use behave mswin or so $VIMRUNTIME/mswin.vim so set up a lot of stuff so that it works more like a regular windows program.
The specific setting you are looking for are:
set select=mouse
this causes the mouse to start select mode instead of visual mode. Select mode causes the selection to be deleted and insert mode to be entered when a printable key is pressed.
As in Prince Goulash's answer
nmap <LeftMouse> <LeftMouse>i
will turn on a sort of click-to-type mode.

In Vim, can you show a tooltip where the cursor is?

Is there a way to display a tooltip (like a popdown menu, but just with text), where the cursor is, using VimScript?
If you mean a tooltip where the mouse cursor is, then you can do this by turning the ballooneval option on and setting the bexpr option to point to a function that returns your required tooltip. This is only available if you're using a vim compiled with +balloon_eval (see :version). Have a look at my (rather basic) tag balloons script for an example.
If you mean a tooltip where the normal vim cursor is, I don't think there's a very clean way to do this. You could create a custom menu with the text that you want (using amenu) and map the command to :nop<CR> and use :popup to display it:
amenu ]MyMenuName.The\ Text\ You\ Want :nop<CR>
popup ]MyMenuName
However, this will only work in the Win32 and GTK GUIs.
I don't know of a way to use the insert mode popup menu (the one used for Ctrl-P and omnicompletion etc) to just display some text. You could abuse the completion method to give your own text as the completion alternative and set menuone in completeopt to allow a single line to be shown, but it would probably overwrite the current text with the contents of the popup menu. It would probably also break omnicompletion!

Resources