Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active April 19, 2016 00:04
Show Gist options
  • Save DanielFGray/741740c46fddc878226d3741c375fb2b to your computer and use it in GitHub Desktop.
Save DanielFGray/741740c46fddc878226d3741c375fb2b to your computer and use it in GitHub Desktop.
[really] poor proof-of-concept [spac]emacs which-key port to vim | demo https://upload.teknik.io/8olPd.webm
" If you want to test this, save it somewhere, open in vim, `:so %`.
" Slowly type <leader>g andand g-commands should happen in the other buffer.
" In the demo webm above, I just slowly typed `<leader>gJ`
" I only added descriptions for a few commands but others should work
" Needs massive re-working to actually be maintainable
" WHY:
" Vim is an amazing tool, but one of it's few weaknesses is discoverability.
" While it's great to have a tool that you can continually learn new things about,
" learning those things would ideally happen sooner than later. Spacemacs ships with "which-key"
" that will show you hints about what key binds would make sense after other key binds. The user
" types `g` and and spacemacs shows a list of valid `g` commands. The idea here is,
" pushing <leader> shows you a list of commands valid in normal mode, pushing another key
" should show valid commands for that key..
" There are things like `:Unite mapping` that show lists of maps, but that only lists things
" that the user has added, it doesn't list default binds, and doesn't give any description about
" what the map should do..
function! s:Discover() abort
let string = ''
" TODO: symbols,usermaps
let keys = {
\ 'q': 'macro',
\ 'Q': 'start Ex mode',
\ 'w': 'move forward a word',
\ 'W': 'move forward a WORD',
\ 'e': 'move to end of word',
\ 'E': 'move to end of WORD',
\ 'r': 'replace char under cursor',
\ 'R': 'start replace mode',
\ 't': 'move cursor forward til char',
\ 'T': 'move cursor backward til char',
\ 'y': 'yank [motion]',
\ 'Y': 'yank line',
\ 'u': 'undo',
\ 'U': 'undo changes to line',
\ 'i': 'insert mode at cursor',
\ 'I': 'insert mode at beginning of line',
\ 'o': 'insert mode below cursor',
\ 'O': 'insert mode above cursor',
\ 'p': 'paste at cursor',
\ 'P': 'paste before cursor',
\ 'a': 'insert mode after cursor',
\ 'A': 'insert mode at end of line',
\ 's': 'substitute char under cursor',
\ 'S': 'substitute current line',
\ 'd': 'delete [motion]',
\ 'D': 'delete to end of line',
\ 'f': 'move cursor forward to char',
\ 'F': 'move cursor backward to char',
\ 'g': 'general',
\ 'G': 'move cursor to end of file',
\ 'h': 'move left one char',
\ 'H': 'move cursor to top of screen',
\ 'j': 'move down one line',
\ 'J': 'join line below to current',
\ 'k': 'move up one line',
\ 'K': 'show man page for word under cursor',
\ 'l': 'move right one char',
\ 'L': 'move cursor to bottom of screen',
\ 'z': 'folds',
\ 'Z': 'quit',
\ 'x': 'delete char under cursor',
\ 'X': 'delete char before cursor',
\ 'c': 'change [motion]',
\ 'C': 'change to end of line',
\ 'v': 'visually select chars',
\ 'V': 'visually select lines',
\ 'b': 'move back a word',
\ 'B': 'move back a WORD',
\ 'n': 'move cursor to next match',
\ 'N': 'move cursor to previous match',
\ 'm': 'mark',
\ 'M': 'move cursor to middle of screen'
\ }
split
enew
call s:PrintMapList(keys)
nnoremap <silent><buffer> <Esc> <C-w>q
setlocal bt=nofile bh=wipe nobl noswf nonu nornu nolist
silent! redraw!
let char = nr2char(getchar())
if exists('*s:Discover_' . char)
execute 'call s:Discover_' . char . '()'
else
wincmd q
execute 'normal ' . char
endif
silent! redraw!
endfunction
command! -nargs=* Discover call s:Discover()
nnoremap <Leader> :Discover<CR>
function! s:Discover_g() abort
" FIXME: this is tiresome... scrape help files?
let g_keys = {
\ 'ge': 'go backwards to end of previous word',
\ 'gE': 'go backwards to end of previous WORD',
\ 'gJ': 'join lines without inserting space'
\ }
call s:PrintMapList(g_keys)
let char = nr2char(getchar())
wincmd q
execute 'normal g' . char
silent! redraw!
endfunction
function! s:PrintMapList(list) abort
%d
for i in items(a:list)
$put =join(i, ' - ')
endfor
if empty(getline('1'))
1d
endif
wincmd H
vert resize 60
silent! redraw!
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment