From cffb0e0191f0ef921761e3d1902831389828612d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 18 Aug 2015 18:14:29 +0900 Subject: [PATCH] Add Commands command --- README.md | 1 + plugin/fzf.vim | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 047b441..9da2c65 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ List of commands | `Locate PATTERN` | `locate` command output | | `History` | `v:oldfiles` and open buffers | | `Snippets` | Snippets ([UltiSnips][us]) | +| `Commands` | User-defined commands | - All commands except `Colors` support `CTRL-T` / `CTRL-X` / `CTRL-V` key bindings to open in a new tab, a new split, or in a new vertical split. diff --git a/plugin/fzf.vim b/plugin/fzf.vim index caff396..464a533 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -364,6 +364,34 @@ endfunction command! -bang Snippets call s:snippets(0) +" ------------------------------------------------------------------ +" Commands +" ------------------------------------------------------------------ +let s:nbs = nr2char(0x2007) + +function! s:format_cmd(line) + return substitute(a:line, '\C \([A-Z]\S*\) ', + \ '\=s:nbs.s:yellow(submatch(1), 1).s:nbs', '') +endfunction + +function! s:command_sink(cmd) + let cmd = matchstr(a:cmd, '\C[A-Z]\S*\ze'.s:nbs) + call feedkeys(':'.cmd.(a:cmd[0] == '!' ? '' : ' ')) +endfunction + +function! s:commands(bang) + redir => cout + silent command + redir END + let list = split(cout, "\n") + call s:fzf({ + \ 'source': extend(list[0:0], map(list[1:], 's:format_cmd(v:val)')), + \ 'sink': function('s:command_sink'), + \ 'options': '--ansi --header-lines 1 -x --prompt "Commands> " -n2 -d'.s:nbs}, a:bang) +endfunction + +command! -bang Commands call s:commands(0) + " ---------------------------------------------------------------------------- " Completion helper " ----------------------------------------------------------------------------