DEV Community

Linwei
Linwei

Posted on

7 1

Vim 2021: Add A Context Menu in You Vim

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

Setup

With the help of the ui-extension plugin quickui, it can be simply defined like this:

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>
Enter fullscreen mode Exit fullscreen mode

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

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 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:

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
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

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

You can also setup a context menu for defx.nvim:

Alt Text

This made defx more user-friendly.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (1)

Collapse
 
khoahuynhdev profile image
Khoa Huỳnh

Great, this is what my vim is missing.

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay