Skip to content
Myr

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

2 files+123 added−40 removed

modified:plugin/fzf.vim+107 added-33 removed
19 unmodified lines
20" 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.2324let s:min_tmux_height = 325let s:default_tmux_height = 152627let s:cpo_save = &cpo28set cpo&vim2930call system('type fzf')
8 unmodified lines
39else40 let s:exec = 'fzf'41endif4243function! s:shellesc(arg)44 return '"'.substitute(a:arg, '"', '\\"', 'g').'"'45endfunction4647function! s:escape(path)48 return substitute(a:path, ' ', '\\ ', 'g')49endfunction5051function! 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')6061 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.result7767 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 endif84endfunction8579 let lines = readfile(result)86function! 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 endif91endfunction9293function! s:popd(dict)94 if has_key(a:dict, 'prev_dir')95 execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))96 endif97endfunction9899function! 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 endif109endfunction110111function! 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:temps125126 augroup fzf_tmux127 autocmd!128 autocmd VimResized * nested call s:tmux_check()129 augroup END130endfunction131132function! s:tmux_check()133 let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')134135 if index(panes, s:pane) < 0136 augroup fzf_tmux137 autocmd!138 augroup END139140 call s:callback(s:dict, s:temps, 1)141 redraw142 endif143endfunction144145function! s:callback(dict, temps, cd)146 if !filereadable(a:temps.result)147 let lines = []148 else149 if a:cd | call s:pushd(a:dict) | endif150151 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 endif162163 for tf in values(a:temps)164 silent! call delete(tf)165 endfor166167 call s:popd(a:dict)168169 return lines170endfunction171101function! s:cmd(...)172function! 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))182endfunction183110command! -nargs=* -complete=dir FZF call s:cmd(<f-args>)184command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)185186let &cpo = s:cpo_save187unlet s:cpo_save188
modified:README.md+16 added-7 removed
288 unmodified lines
289290Usage as Vim plugin291-------------------292293### `:FZF`293(fzf is a command-line utility, naturally it is only accessible in terminal Vim)294295If you have set up fzf as a Vim plugin, `:FZF` command will be added.295### `:FZF[!]`296297If you have set up fzf for Vim, `:FZF` command will be added.298299```vim300" Look for files under current directory301:FZF302
3 unmodified lines
306" With options307:FZF --no-sort -m /tmp308```309308Note that environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`310Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`311also apply here.312313If you're on a tmux session, `:FZF`, will launch fzf in a new split-window whose314height can be adjusted with `g:fzf_tmux_height` (default: 15). However, the bang315version (`:FZF!`) will always start in fullscreen.316317### `fzf#run([options])`318319For more advanced uses, you can call `fzf#run()` function which returns the list320of the selected items.
7 unmodified lines
328| `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 |333334#### Examples335336If `sink` option is not given, `fzf#run` will simply return the list.
13 unmodified lines
350We can also use a Vim list as the source as follows:351352```vim353" Choose a color scheme with fzf347call fzf#run({354nnoremap <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```363364`sink` option can be a function reference. The following example creates a365handy mapping that selects an open buffer.
10 unmodified lines
376function! g:bufopen(e)377 execute 'buffer '. matchstr(a:e, '^[ 0-9]*')378endfunction379372nnoremap <Leader><Enter> :call fzf#run({380nnoremap <silent> <Leader><Enter> :call fzf#run({381\ 'source': g:buflist(),382\ 'sink': function('g:bufopen'),383\ 'options': '+m +s',384\ 'tmux': 15385\ })<CR>386```387388Tips
41 unmodified lines