Skip to content

Instantly share code, notes, and snippets.

@tyru
Forked from Shougo/ambicmd.vim
Created April 4, 2010 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyru/355371 to your computer and use it in GitHub Desktop.
Save tyru/355371 to your computer and use it in GitHub Desktop.
ambicmd.vim - You can use ambiguous command.
" You can use ambiguous command.
" Version: 0.2.0
" Author : thinca <thinca+vim@gmail.com>
" Shougo <Shougo.Matsu (at) gmail.com>
" tyru <tyru.exe@gmail.com>
" License: Creative Commons Attribution 2.1 Japan License
" <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
" Install: copy to autoload/ambicmd.vim
let s:save_cpo = &cpo
set cpo&vim
" Expand ambiguous command.
" Example:
" autocmd CmdwinEnter * call s:init_cmdwin()
"function! s:init_cmdwin()
"" Ambicmd.
"inoremap <buffer><expr> <Space> ambicmd#expand("\<Space>")
"inoremap <buffer><expr> <CR> ambicmd#expand("\<CR>")
"startinsert!
"endfunction"}}}
"
" cnoremap <expr> <Space> ambicmd#expand("\<Space>")
" cnoremap <expr> <CR> ambicmd#expand("\<CR>")
function! ambicmd#expand(key)
" TODO: The check is incomplete.
let line = mode() ==# 'c' ? getcmdline() : getline('.')
let pos = mode() ==# 'c' ? getcmdpos() : col('.') - 1
let cmd = matchstr(line[:pos], '^\S\{-}\zs\a\w*')
let state = exists(':' . cmd)
if cmd == '' || state == 1 || state == 2 || state == 3
return a:key
endif
" Get command list.
redir => cmdlistredir
silent! command
redir END
let cmdlist = map(split(cmdlistredir, "\n")[1:],
\ 'matchstr(v:val, ''\a\w*'')')
" Search matching.
for pat in [
\ '\c^' . cmd . '$',
\ cmd,
\ '\C^' . substitute(toupper(cmd), '.', '\0\\l*', 'g') . '$',
\ '\C' . substitute(toupper(cmd), '.', '\0\\l*', 'g'),
\ '.*' . substitute(cmd, '.', '\0.*', 'g')]
let filtered = filter(copy(cmdlist), 'v:val =~? ' . string(pat))
if len(filtered) == 1
let ret = repeat("\<BS>", strlen(cmd)) . filtered[0] . a:key
if mode() !=# 'c'
let ret = (pumvisible() ? "\<C-y>" : '') . ret
endif
return ret
endif
endfor
return a:key
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment