Skip to content

Instantly share code, notes, and snippets.

@FranckPachot
Last active November 7, 2025 09:03
Show Gist options
  • Select an option

  • Save FranckPachot/a6db24db92867960aa4596c5838801ce to your computer and use it in GitHub Desktop.

Select an option

Save FranckPachot/a6db24db92867960aa4596c5838801ce to your computer and use it in GitHub Desktop.
.vimrc to tmux send-keys the current line
function! SendLineToTmux(no_return) abort
let target = ''
let restore_focus = 0
let current_pane = ''
" Only do targeting / restore logic if inside tmux (opening the script in the last pane)
if exists('$TMUX')
let current_pane = substitute(system('tmux display-message -p "#{pane_id}"'), '\n$', '', '')
" Jump to last active pane to get its ID, then target it
call system('tmux last-pane')
let target_pane = substitute(system('tmux display-message -p "#{pane_id}"'), '\n$', '', '')
let target = '-t ' . target_pane
let restore_focus = 1
endif
let line = getline('.')
" Decide what command to run
if line =~ '^$'
" Empty line: send Enter
call system('tmux send-keys '.target.' C-M')
elseif line =~ '^\(---\|###\|///\)\s\+tmux'
" Lines starting with --- ... tmux → run directly
let cmd = substitute(line, '^\(---\|###\|///\)\s\+', '', '')
call system(cmd)
else
" Normal sending
if a:no_return
call system('tmux send-keys '.target.' -- '.shellescape(line))
else
call system('tmux send-keys '.target.' -- '.shellescape(line).' C-M')
endif
endif
" Restore focus only if needed
if restore_focus
call system('tmux select-pane -t ' . current_pane)
endif
endfunction
# key mappings
" Space: send without Enter (I use space because that's what my clicker sends)
nnoremap <space> :call SendLineToTmux(1)<CR>j
" Ctrl-Space (may appear as <C-@>): send with Enter
nnoremap <C-@> :call SendLineToTmux(0)<CR>
nnoremap C-Space> :call SendLineToTmux(0)<CR>
# other commands
set paste
set tabstop=2 shiftwidth=2 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment