3

I want to add a new item to Vim context menu. I select a word, right click on the word, select the new menu item, auto delete rows contain selected word.

How should i do?

5

The menu you are referring to is called "PopUp". To create new menu entries you use the command menu (It has all the same variations as map, plus a special one amenu for all modes)

The following command will add a PopUp menu that will delete all rows containing the word under the cursor when you right click.

noremenu PopUp.Delete\ Rows :<c-u>g/\<<c-r><c-w>\>/d<cr>

So this creates an menu entry called "Delete Rows", when you click it will run :<c-u>g/\<<c-r><c-w>\>/d<cr>. The command it is running is a global command that deletes every line that contains the word under the cursor. The word under the cursor is retrieved with <c-r><c-w>.

To change the location in the list you need to modify the priority of the menu command. To do this you can place .<number>. (The dot represents that we are in a submenu). The number represents how high in the list the menu item will be. So

noremenu .1 PopUp.Delete\ Rows :<c-u>g/\<<c-r><c-w>\>/d<cr>

will probably be placed first in the list.

To find out the priorities of the entries in the PopUp menu you can run

menu PopUp

and look at the numbers left of the menu item. If you don't specify a priority, the menu item will be assigned a priority of 500.

Required readings :help menu, :help sub-menu-priority, :help c_CTRL-R_CTRL-W and :help :global.


After doing all of this, you should take note that learning how to use vim commands would be faster. For instance all you need to do is hit * on the word you want to delete and then run :g//d to delete all lines containing that word. * searches for the word under the cursor, and if you leave out the regex in g it uses the last search.

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.