Skip to content

Instantly share code, notes, and snippets.

@chebert
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save chebert/fc3b24baaf1026c76de9 to your computer and use it in GitHub Desktop.

Select an option

Save chebert/fc3b24baaf1026c76de9 to your computer and use it in GitHub Desktop.
Send keys to another buffer in a tmux session. Pretty handy.
nmap <silent> <leader>s :set opfunc=SendToTmux<CR>g@
nmap <silent> <leader>ss :call SendLineToTmux()<CR>
vmap <silent> <leader>s :<C-U>call SendToTmux(visualmode(), 1)<CR>
function! SendLineToTmux()
let line = line('.')
let col = col('.')
" TODO: this assumes the leader is ','
:normal 0,s$""
echo system("tmux send-keys -t " . g:tmux_session_name . ":" . g:tmux_window_name . " Enter")
call cursor(line, col)
endfunction
function! SendToTmux(type, ...)
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
if !exists("g:tmux_session_name")
let g:tmux_session_name=input("session name: ")
end
if !exists("g:tmux_window_name")
let g:tmux_window_name=input("window name: ")
end
if a:0 " Invoked from Visual mode, use '< and '> marks.
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
silent exe "normal! '[V']y"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]y"
else
silent exe "normal! `[v`]y"
endif
let sub = substitute(@@, "'", "'\\\\''", 'g')
" Escape a semicolon only if its the last character. Go figure.
let sub = substitute(sub, ";$", "\\\\;", 'g')
echo system("tmux send-keys -t " . g:tmux_session_name . ":" . g:tmux_window_name . " -l '" . sub . "'")
let @@ = reg_save
let &selection = sel_save
endfunction
" NOTE: Example: some SML buffer.
" Visually select, and type <leader>s.
" You will prompted for a window name.
" Type: sml <RET> (or the name of another window in the tmux session)
" You will prompted for a tmux session name.
" Type: 0 <RET> (or the name of tmux session)
" check the sml buffer, and see the text evaulated!
fun fact word = explode word;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment