Vim 2021: Add A Context Menu in You Vim
Tags: vim, productivity, linux, editor
Published: 2021-01-27 07:58:12

---
title: Vim 2021: Add A Context Menu in You Vim
published: true
description: 
tags: vim, productivity, linux, editor
---

A nice looking context menu is very useful when you have something to do with current word/line under cursor. And it can also remind you when you forget your keymaps:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zd5op8x68wv9k8gh7n5b.gif)

## Setup

With the help of the ui-extension plugin [quickui](https://github.com/skywind3000/vim-quickui), it can be simply defined like this:

```VimL
Plug 'skywind3000/vim-quickui'

" define your context menu as a list of (text, command) pairs
let g:context_menu_k = [
            \ ["&Help Keyword\t\\ch", 'echo expand("<cword>")' ],
            \ ["&Signature\t\\cs", 'echo 101'],
            \ ['-'],
            \ ["Find in &File\t\\cx", 'exec "/" . expand("<cword>")' ],
            \ ["Find in &Project\t\\cp", 'exec "vimgrep " . expand("<cword>") . "*"' ],
            \ ["Find in &Defintion\t\\cd", 'YcmCompleter GotoDefinition' ],
            \ ["Search &References\t\\cr", 'YcmCompleter GoToReferences'],
            \ ['-'],
            \ ["&Documentation\t\\cm", 'exec "PyDoc " . expand("<cword>")'],
            \ ]

" map 'K' to display the context menu
nnoremap <silent>K :call quickui#tools#clever_context('k', g:context_menu_k, {})<cr>
```

Then when you press `K`, it will display around the cursor:

![](https://skywind3000.github.io/images/p/quickui/context.png)

Context menu is a good place to organize your LSP commands.

## Usage

- navigate the items by `j`/`k` or arrow keys.
- accept an item by `Enter` or `Space` or mouse left-click.
- press `ESC` to quit.
- Hot keys can be defined by a `&`.

The border and color are also customizable, check the quickui [doc](https://github.com/skywind3000/vim-quickui) for more.

## Plugin Dedicated Context

Context menu can also be used to enhance plugin experience, you can setup some buffer local keymap in the plugin buffer:

```VimL
let g:context_menu_git = [
      \ ["&Stage (add)\ts", 'exec "normal s"' ],
      \ ["&Unstage (reset)\tu", 'exec "normal u"' ],
      \ ["&Toggle stage/unstage\t-", 'exec "normal -"' ],
      \ ["Unstage &Everything\tU", 'exec "normal U"' ],
      \ ["D&iscard change\tX", 'exec "normal X"' ],
      \ ["--"],
      \ ["Inline &Diff\t=", 'exec "normal ="' ],
      \ ["Diff S&plit\tdd", 'exec "normal dd"' ],
      \ ["Diff &Horizontal\tdh", 'exec "normal dh"' ],
      \ ["Diff &Vertical\tdv", 'exec "normal dv"' ],
      \ ["--"],
      \ ["&Open File\t<CR>", 'exec "normal \<cr>"' ],
      \ ["Open in New Split\to", 'exec "normal o"' ],
      \ ["Open in New Vsplit\tgO", 'exec "normal gO"' ],
      \ ["Open in New Tab\tO", 'exec "normal O"' ],
      \ ["Open in &Preview\tp", 'exec "normal p"' ],
      \ ["--"],
      \ ["&Commit\tcc", 'exec "normal cc"' ],
      \ ]

function! s:setup_fugitive()
	nnoremap <silent><buffer>K :call quickui#tools#clever_context('g', g:context_menu_git, {})<cr>
endfunc

augroup MenuEvents
	au!
	au FileType fugitive call s:setup_fugitive()
augroup END
```

When you are using fugitive, you can press `K` to display a fugitive menu:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e3lklalvoaafv31za09k.png)

No need to remember these seldom used fugitive keymaps, just `K` is totally enough. 

You can also setup a context menu for [defx.nvim](https://github.com/Shougo/defx.nvim):

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qj8p0mba7fxzcrwlttav.jpg)

This made defx more user-friendly.