From e865144aced734a942fd6876fe0c8402be214659 Mon Sep 17 00:00:00 2001
From: Junegunn Choi <junegunn.c@gmail.com>
Date: Sun, 29 Mar 2015 03:00:32 +0900
Subject: [PATCH] [vim] Implement ctrlp-compatible key bindings (#139, #96,
 #61)

---
 README.md      |  4 ++++
 plugin/fzf.vim | 26 ++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index cb18e4c5..55a5ea4c 100644
--- a/README.md
+++ b/README.md
@@ -275,6 +275,10 @@ If you have set up fzf for Vim, `:FZF` command will be added.
 :FZF --no-sort -m /tmp
 ```
 
+Similarly to [ctrlp.vim](https://github.com/kien/ctrlp.vim), use enter key,
+`CTRL-T`, `CTRL-X` or `CTRL-V` to open selected files in the current window,
+in new tabs, in horizontal splits, or in vertical splits respectively.
+
 Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`
 also apply here.
 
diff --git a/plugin/fzf.vim b/plugin/fzf.vim
index 713dfff9..9c8ae624 100644
--- a/plugin/fzf.vim
+++ b/plugin/fzf.vim
@@ -26,6 +26,7 @@ let s:launcher = 'xterm -e bash -ic %s'
 let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'
 let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
 let s:fzf_tmux = expand('<sfile>:h:h').'/bin/fzf-tmux'
+let s:legacy = 0
 
 let s:cpo_save = &cpo
 set cpo&vim
@@ -40,6 +41,7 @@ function! s:fzf_exec()
         let s:exec = path[0]
       elseif executable(s:fzf_rb)
         let s:exec = s:fzf_rb
+        let s:legacy = 1
       else
         call system('type fzf')
         if v:shell_error
@@ -221,7 +223,7 @@ function! s:callback(dict, temps)
         if type(a:dict.sink) == 2
           call a:dict.sink(line)
         else
-          execute a:dict.sink.' '.s:escape(line)
+          execute a:dict.sink s:escape(line)
         endif
       endfor
     endif
@@ -238,6 +240,9 @@ endfunction
 
 function! s:cmd(bang, ...) abort
   let args = copy(a:000)
+  if !s:legacy
+    let args = add(args, '--expect=ctrl-t,ctrl-x,ctrl-v')
+  endif
   let opts = {}
   if len(args) > 0 && isdirectory(expand(args[-1]))
     let opts.dir = remove(args, -1)
@@ -245,7 +250,24 @@ function! s:cmd(bang, ...) abort
   if !a:bang
     let opts.down = get(g:, 'fzf_tmux_height', s:default_tmux_height)
   endif
-  call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
+
+  if s:legacy
+    call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
+  else
+    let output = fzf#run(extend({ 'options': join(args) }, opts))
+    if empty(output)
+      return
+    endif
+    let key = remove(output, 0)
+    if     key == 'ctrl-t' | let cmd = 'tabedit'
+    elseif key == 'ctrl-x' | let cmd = 'split'
+    elseif key == 'ctrl-v' | let cmd = 'vsplit'
+    else                   | let cmd = 'e'
+    endif
+    for item in output
+      execute cmd s:escape(item)
+    endfor
+  endif
 endfunction
 
 command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)
-- 
GitLab