From 813b58b3eab36406f0e064dad4eae3c5a57a2f49 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 22 Aug 2015 01:00:31 +0900 Subject: [PATCH] Add mappings --- README.md | 41 +++++++++++++++++++++++++++++------------ plugin/fzf.vim | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4dfc4f0..004c5df 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -fzf.vim -======= +fzf :heart: vim +=============== -A set of [fzf][fzf]-based Vim commands. +A set of [fzf][fzf]-based commands and mappings. Rationale --------- @@ -12,10 +12,10 @@ write their own Vim commands with it. However, I've learned that many users of fzf are not familiar with Vimscript and are looking for the "default" implementation of the features they can find in the alternative Vim plugins. -This repository is a bundle of fzf-based commands extracted from my -[.vimrc][vimrc] to address such needs. The commands are opinionated and not -designed to be extremely flexible or configurable, and they are not guaranteed -to be backward-compatible. +This repository is a bundle of fzf-based commands and mappings extracted from +my [.vimrc][vimrc] to address such needs. They are *not* designed to be +flexible or configurable, nor are not guaranteed to be backward-compatible at +the moment, so you might want to treat this repository as a reference. Installation ------------ @@ -27,8 +27,8 @@ Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' } Plug 'junegunn/fzf.vim' ``` -List of commands ----------------- +Commands +-------- | Command | List | | --- | --- | @@ -44,7 +44,7 @@ List of commands | `Snippets` | Snippets ([UltiSnips][us]) | | `Commands` | User-defined commands | -- All commands except `Colors` support `CTRL-T` / `CTRL-X` / `CTRL-V` key +- Most commands support `CTRL-T` / `CTRL-X` / `CTRL-V` key bindings to open in a new tab, a new split, or in a new vertical split. - Bang-versions of the commands (e.g. `Ag!`) will open fzf in fullscreen @@ -61,8 +61,25 @@ let g:fzf_action = { let g:fzf_layout = { 'down': '40%' } ``` -Fuzzy completion helper ------------------------ +Mappings +-------- + +| Mapping | Description | +| --- | --- | +| `(fzf-complete-word)` | `cat /usr/share/dict/words` | +| `(fzf-complete-path)` | Path completion using `find` (file + dir) | +| `(fzf-complete-file)` | File completion using `find` | +| `(fzf-complete-file-ag)` | File completion using `ag` | + +### Usage + +```vim +imap (fzf-complete-word) +imap (fzf-complete-path) +imap (fzf-complete-file-ag) +``` + +### Completion helper `fzf#complete` is a helper function for creating custom fuzzy completion using fzf. If the first parameter is a command string or a Vim list, it will be used diff --git a/plugin/fzf.vim b/plugin/fzf.vim index 9d15757..3149e31 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -482,6 +482,53 @@ function! fzf#complete(...) return '' endfunction +" ---------------------------------------------------------------------------- +" (fzf-complete-word) +" ---------------------------------------------------------------------------- +inoremap (fzf-complete-word) fzf#complete('cat /usr/share/dict/words') + +" ---------------------------------------------------------------------------- +" (fzf-complete-path) +" (fzf-complete-file) +" (fzf-complete-file-ag) +" ---------------------------------------------------------------------------- +function! s:file_split_prefix(prefix) + let expanded = expand(a:prefix) + return isdirectory(expanded) ? + \ [expanded, + \ substitute(a:prefix, '/*$', '/', ''), + \ ''] : + \ [fnamemodify(expanded, ':h'), + \ substitute(fnamemodify(a:prefix, ':h'), '/*$', '/', ''), + \ fnamemodify(expanded, ':t')] +endfunction + +function! s:file_source(prefix) + let [dir, head, tail] = s:file_split_prefix(a:prefix) + return printf( + \ "cd %s && ".s:file_cmd." | sed 's:^:%s:'", + \ shellescape(dir), empty(a:prefix) || a:prefix == tail ? '' : head) +endfunction + +function! s:file_options(prefix) + let [_, head, tail] = s:file_split_prefix(a:prefix) + return printf('--prompt %s --query %s', shellescape(head), shellescape(tail)) +endfunction + +function! s:complete_file(bang, command) + let s:file_cmd = a:command + return fzf#complete(extend({ + \ 'prefix': '\S*$', + \ 'source': function('file_source'), + \ 'options': function('file_options')}, a:bang ? {} : s:win())) +endfunction + +inoremap (fzf-complete-path) +\ complete_file(0, "find . -path '*/\.*' -prune -o -print \| sed '1d;s:^..::'") +inoremap (fzf-complete-file) +\ complete_file(0, "find . -path '*/\.*' -prune -o -type f -print -o -type l -print \| sed '1d;s:^..::'") +inoremap (fzf-complete-file-ag) complete_file(0, "ag -l -g ''") + " ------------------------------------------------------------------ let &cpo = s:cpo_save unlet s:cpo_save