Skip to content

Instantly share code, notes, and snippets.

@tacahiroy
Created August 2, 2012 22:07
Show Gist options
  • Save tacahiroy/3241096 to your computer and use it in GitHub Desktop.
Save tacahiroy/3241096 to your computer and use it in GitHub Desktop.
Vim easy tmux integration
" tmux: just send keys against tmux
let s:tmux = {}
let s:tmux.last_cmd = ''
function! s:tmux.is_installed()
call system('which tmux')
return v:shell_error == 0
endfunction
function! s:tmux.is_running()
call system('tmux ls -F "#{session_name}:#{session_attached}" | grep :1')
return v:shell_error == 0
endfunction
function! s:tmux.run(cmd, ...)
let split = get(a:, 1, 0)
let run_in_vim = get(a:, 2, 0)
let self.last_cmd = a:cmd
if self.is_running()
if split
let res = system(printf('tmux splitw -v "%s"', a:cmd))
if v:shell_error
echomsg res
endif
else
let enter = (a:cmd =~# '^\^[a-zA-Z]$' ? '' : 'Enter')
call system(printf('`tmux send "%s" %s`', a:cmd, enter))
endif
elseif run_in_vim
execute ':!' . a:cmd
else
echohl ErrorMsg | echo 'ERR: tmux is not running' | echohl NONE
return
endif
endfunction
function! s:tmux.operate(cmd)
if self.is_running()
call system(printf('`tmux "%s"`', a:cmd))
endif
endfunction
if s:tmux.is_installed()
command! -nargs=+ TMRun call s:tmux.run(<q-args>)
command! -nargs=0 TMInt call s:tmux.run('^C')
command! -nargs=0 TMClear call s:tmux.run('^L')
command! -nargs=0 TMRunAgain echo s:tmux.last_cmd | call s:tmux.run(s:tmux.last_cmd)
command! -nargs=0 TMNextWindow call s:tmux.operate('next-window')
command! -nargs=0 TMPrevWindow call s:tmux.operate('previous-window')
nnoremap <Leader>r :<C-u>TMRun<Space>
nnoremap <Leader>! :<C-u>TMRunAgain<Cr>
nnoremap <Leader>c :<C-u>TMInt<Cr>
nnoremap <Leader>> :<C-u>TMNextWindow<Cr>
nnoremap <Leader>< :<C-u>TMPrevWindow<Cr>
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment