Vim plugin: tmux integration
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 4908171 · Browse files · 2 files·+123 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+123 added−40 removed
- other:+123 added−40 removed
Provenance
- Commit
2b34665- Tree
c917fd3- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4908171
2 files changed+123 added−40 removed
modified:plugin/fzf.vim+107 added-33 removed
19 unmodified lines20 " LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21 " OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22 " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23 24+let s:min_tmux_height = 325+let s:default_tmux_height = 1526+27 let s:cpo_save = &cpo28 set cpo&vim29 30 call system('type fzf')8 unmodified lines39 else40 let s:exec = 'fzf'41 endif42 43+function! s:shellesc(arg)44+ return '"'.substitute(a:arg, '"', '\\"', 'g').'"'45+endfunction46+47 function! s:escape(path)48 return substitute(a:path, ' ', '\\ ', 'g')49 endfunction50 51 function! fzf#run(...) abort52+ if has('gui_running')53+ echohl Error54+ echo 'GVim is not supported'55+ return []56+ endif57 let dict = exists('a:1') ? a:1 : {}46- let temps = [tempname()]47- let result = temps[0]58+ let temps = { 'result': tempname() }59 let optstr = get(dict, 'options', '')49- let cd = has_key(dict, 'dir')60 61 if has_key(dict, 'source')62 let source = dict.source63 let type = type(source)64 if type == 165 let prefix = source.'|'66 elseif type == 357- let input = add(temps, tempname())[-1]58- call writefile(source, input)59- let prefix = 'cat '.s:escape(input).'|'67+ let temps.input = tempname()68+ call writefile(source, temps.input)69+ let prefix = 'cat '.s:shellesc(temps.input).'|'70 else71 throw 'Invalid source type'72 endif73 else74 let prefix = ''75 endif76+ let command = prefix.s:exec.' '.optstr.' > '.temps.result77 67- try68- if cd69- let cwd = getcwd()70- execute 'chdir '.s:escape(dict.dir)71- endif72- silent !clear73- execute 'silent !'.prefix.s:exec.' '.optstr.' > '.result74- redraw!75- if v:shell_error76- return []77- endif78+ if exists('$TMUX') && has_key(dict, 'tmux') &&79+ \ dict.tmux > 0 && winheight(0) >= s:min_tmux_height80+ return s:execute_tmux(dict, command, temps)81+ else82+ return s:execute(dict, command, temps)83+ endif84+endfunction85 79- let lines = readfile(result)86+function! s:pushd(dict)87+ if has_key(a:dict, 'dir')88+ let a:dict.prev_dir = getcwd()89+ execute 'chdir '.s:escape(a:dict.dir)90+ endif91+endfunction92 93+function! s:popd(dict)94+ if has_key(a:dict, 'prev_dir')95+ execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))96+ endif97+endfunction98+99+function! s:execute(dict, command, temps)100+ call s:pushd(a:dict)101+ silent !clear102+ execute 'silent !'.a:command103+ redraw!104+ if v:shell_error105+ return []106+ else107+ return s:callback(a:dict, a:temps, 0)108+ endif109+endfunction110+111+function! s:execute_tmux(dict, command, temps)112+ if has_key(a:dict, 'dir')113+ let command = 'cd '.s:escape(a:dict.dir).' && '.a:command114+ else115+ let command = a:command116+ endif117+ let height = a:dict.tmux118+ let s:pane = substitute(119+ \ system(120+ \ printf(121+ \ 'tmux split-window -l %d -P -F "#{pane_id}" %s',122+ \ height, s:shellesc(command))), '\n', '', 'g')123+ let s:dict = a:dict124+ let s:temps = a:temps125+126+ augroup fzf_tmux127+ autocmd!128+ autocmd VimResized * nested call s:tmux_check()129+ augroup END130+endfunction131+132+function! s:tmux_check()133+ let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')134+135+ if index(panes, s:pane) < 0136+ augroup fzf_tmux137+ autocmd!138+ augroup END139+140+ call s:callback(s:dict, s:temps, 1)141+ redraw142+ endif143+endfunction144+145+function! s:callback(dict, temps, cd)146+ if !filereadable(a:temps.result)147+ let lines = []148+ else149+ if a:cd | call s:pushd(a:dict) | endif150+151+ let lines = readfile(a:temps.result)81- if has_key(dict, 'sink')152+ if has_key(a:dict, 'sink')153 for line in lines83- if type(dict.sink) == 284- call dict.sink(line)154+ if type(a:dict.sink) == 2155+ call a:dict.sink(line)156 else86- execute dict.sink.' '.s:escape(line)157+ execute a:dict.sink.' '.s:escape(line)158 endif159 endfor160 endif90- return lines91- finally92- if cd93- execute 'chdir '.s:escape(cwd)94- endif95- for tf in temps96- silent! call delete(tf)97- endfor98- endtry161+ endif162+163+ for tf in values(a:temps)164+ silent! call delete(tf)165+ endfor166+167+ call s:popd(a:dict)168+169+ return lines170 endfunction171 101-function! s:cmd(...)172+function! s:cmd(bang, ...) abort173 let args = copy(a:000)174 let opts = {}175 if len(args) > 0 && isdirectory(expand(args[-1]))176 let opts.dir = remove(args, -1)177 endif178+ if !a:bang179+ let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height)180+ endif181 call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))182 endfunction183 110-command! -nargs=* -complete=dir FZF call s:cmd(<f-args>)184+command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)185 186 let &cpo = s:cpo_save187 unlet s:cpo_save188 modified:README.md+16 added-7 removed
288 unmodified lines289 290 Usage as Vim plugin291 -------------------292 293-### `:FZF`293+(fzf is a command-line utility, naturally it is only accessible in terminal Vim)294 295-If you have set up fzf as a Vim plugin, `:FZF` command will be added.295+### `:FZF[!]`296 297+If you have set up fzf for Vim, `:FZF` command will be added.298+299 ```vim300 " Look for files under current directory301 :FZF302 3 unmodified lines306 " With options307 :FZF --no-sort -m /tmp308 ```309 308-Note that environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`310+Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`311 also apply here.312 313+If you're on a tmux session, `:FZF`, will launch fzf in a new split-window whose314+height can be adjusted with `g:fzf_tmux_height` (default: 15). However, the bang315+version (`:FZF!`) will always start in fullscreen.316+317 ### `fzf#run([options])`318 319 For more advanced uses, you can call `fzf#run()` function which returns the list320 of the selected items.7 unmodified lines328 | `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |329 | `sink` | funcref | Reference to function to process each selected item |330 | `options` | string | Options to fzf |331 | `dir` | string | Working directory |332+| `tmux` | number | Use tmux split if possible with the given height |333 334 #### Examples335 336 If `sink` option is not given, `fzf#run` will simply return the list.13 unmodified lines350 We can also use a Vim list as the source as follows:351 352 ```vim353 " Choose a color scheme with fzf347-call fzf#run({354+nnoremap <silent> <Leader>C :call fzf#run({355 \ 'source':356 \ map(split(globpath(&rtp, "colors/*.vim"), "\n"),357 \ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),358 \ 'sink': 'colo',352-\ 'options': '+m'353-\ })359+\ 'options': '+m',360+\ 'tmux': 15361+\ })<CR>362 ```363 364 `sink` option can be a function reference. The following example creates a365 handy mapping that selects an open buffer.10 unmodified lines376 function! g:bufopen(e)377 execute 'buffer '. matchstr(a:e, '^[ 0-9]*')378 endfunction379 372-nnoremap <Leader><Enter> :call fzf#run({380+nnoremap <silent> <Leader><Enter> :call fzf#run({381 \ 'source': g:buflist(),382 \ 'sink': function('g:bufopen'),383 \ 'options': '+m +s',384+\ 'tmux': 15385 \ })<CR>386 ```387 388 Tips41 unmodified lines