A tiny vim plugin that sends a word/line/paragraph of text from vim into a tmux pane for execution.
git clone https://mcol.xyz/code/tide
Log | Files | Refs | README | LICENSE

tide.vim (4834B)


      1 "------------------------------------------------------------------"
      2 " tide: tmux IDE
      3 "------------------------------------------------------------------"
      4 
      5 " load once
      6 if exists('g:loaded_tide')
      7     finish
      8 endif
      9 let g:loaded_tide = 1
     10 
     11 " command to prefix text
     12 if !exists("g:tmux_cmd")
     13     let g:tmux_cmd = "tmux send -t .+ "
     14 endif
     15 
     16 " main send keys function
     17 function! TmuxSendKeys(keys)
     18     let l:keys = a:keys
     19     "let l:keys = substitute(a:keys, '^[\ ]*', '', 'g') "strip leading whitespace
     20     let l:keys = substitute(l:keys, '	', '    ', 'g') "make tabs spaces
     21     let l:keys = substitute(l:keys, ";$", '; ', 'g') "preserve trailing semicolons
     22     let l:keys = escape(l:keys, '\"$')
     23     "let l:keys = substitute(l:keys, '\', '\\\\', 'g') "escape backslashes
     24     "let l:keys = substitute(l:keys, '\"', '\\\"', 'g') "escape double quotes
     25     "let l:keys = substitute(l:keys, '!', '\!', 'g') "escape esclamation mark
     26     "let l:keys = escape(l:keys, '!')
     27     "let l:keys = substitute(l:keys, '\$', '\\\$', 'g') "escape dollar sign
     28     call system(g:tmux_cmd . "-l \"" . l:keys . "\" &")
     29 endfunction
     30 function! s:TmuxSendEnter()
     31     call system(g:tmux_cmd . "C-m &")
     32 endfunction
     33 function! TmuxSendKeysEnter(keys)
     34     call TmuxSendKeys(a:keys)
     35     call s:TmuxSendEnter()
     36 endfunction
     37 
     38 " send region between two lines
     39 function! TmuxSendLines(top, bottom)
     40     if a:top > a:bottom
     41 	call TmuxSendKeys(getline(a:top))
     42 	call s:TmuxSendEnter()
     43     else
     44 	let l:numcomments = 0
     45 	for l:line in range(a:top, a:bottom)
     46 	    let l:linetext = getline(l:line)
     47 	    if empty(l:linetext)
     48 		let l:numcomments +=1
     49 		if l:numcomments > 1
     50 		    continue
     51 		endif
     52 	    else
     53 		let l:numcomments = 0
     54 	    endif
     55 	    call TmuxSendKeysEnter(l:linetext)
     56 	endfor
     57     endif
     58 endfunction
     59 
     60 " send current paragraph
     61 function! s:TmuxSendParagraph()
     62     let l:top = search('^[\ ]*$\|\%^?', 'cbnW')
     63     if l:top != 1
     64 	let l:top = l:top + 1
     65     endif
     66     let l:bottom = search('^[\ ]*$\|\%$', 'nW')
     67     if l:bottom != line("$")
     68 	let l:bottom = l:bottom - 1
     69     endif
     70     call TmuxSendLines(l:top, l:bottom)
     71 endfunction
     72 
     73 " send current section delimited by double comments
     74 function! s:TmuxSendSection()
     75     " find shortest comment character
     76     if ! exists("b:com")
     77 	let b:com = split(&comments, ",")
     78     endif
     79     let s:scom = substitute(b:com[0], "^.*:", '', '')
     80     for s:i in b:com
     81 	let s:ncom = substitute(s:i, "^.*:", '', '')
     82 	if len(s:ncom) < len(s:scom)
     83 	    let s:scom = s:ncom
     84 	endif
     85     endfor
     86     let s:header = "^\s*" . s:scom . s:scom
     87     let s:top = search(s:header . '\|\%^?', 'cbnW')
     88     let s:bottom = search(s:header . '\|\%$', 'nW')
     89     if s:bottom <= s:top
     90 	let s:bottom = line('$')
     91     elseif s:bottom != line('$')
     92 	let s:bottom = prevnonblank(s:bottom-1)
     93     endif
     94     if s:top == 0
     95 	let s:top = 1
     96     endif
     97     call TmuxSendLines(s:top, s:bottom)
     98 endfunction
     99 
    100 " send visual selection
    101 function! s:TmuxSendVisual()
    102     let [line_start, column_start] = getpos("'<")[1:2]
    103     let [line_end, column_end] = getpos("'>")[1:2]
    104     let lines = getline(line_start, line_end)
    105     if len(lines) == 0
    106 	return ''
    107     endif
    108     let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
    109     let lines[0] = lines[0][column_start - 1:]
    110     for line in lines
    111 	call TmuxSendKeysEnter(line)
    112     endfor
    113 endfunction
    114 
    115 " command and plugin mappings
    116 command! -nargs=+ -complete=command TmuxSendKeys call TmuxSendKeys(<q-args>)
    117 command! -nargs=+ -complete=command TmuxSendKeysEnter call TmuxSendKeysEnter(<q-args>)
    118 command! -nargs=0 TmuxSendVisual call s:TmuxSendVisual()
    119 command! -nargs=0 TmuxSendLine call TmuxSendKeys(getline(".")) | call s:TmuxSendEnter()
    120 command! -nargs=0 TmuxSendParagraph call s:TmuxSendParagraph()
    121 command! -nargs=0 TmuxSendSection call s:TmuxSendSection()
    122 command! -nargs=0 -range TmuxSendLines call TmuxSendLines(<line1>, <line2>)
    123 
    124 silent! xnoremap <unique> <silent> <script> <Plug>TmuxSendVisual :<C-u>TmuxSendVisual<CR>
    125 silent! nnoremap <unique> <silent> <script> <Plug>TmuxSendParagraph :TmuxSendParagraph<CR>
    126 silent! nnoremap <unique> <silent> <script> <Plug>TmuxSendLine :TmuxSendLine<CR>
    127 silent! nnoremap <unique> <silent> <script> <Plug>TmuxSendWord :call TmuxSendKeysEnter(expand("<cword>"))<CR>
    128 silent! nnoremap <unique> <silent> <script> <Plug>TmuxSendWORD :call TmuxSendKeysEnter(expand("<cWORD>"))<CR>
    129 silent! nnoremap <unique> <silent> <script> <Plug>TmuxSendSection :TmuxSendSection<CR>
    130 
    131 " default keybindings
    132 if !exists("g:tide_no_default_keys")
    133     silent! xmap <unique> <silent> <F9> <Plug>TmuxSendVisual
    134     silent! nmap <unique> <silent> <F9> <Plug>TmuxSendParagraph
    135     silent! nmap <unique> <silent> <F8> <Plug>TmuxSendLine
    136     silent! nmap <unique> <silent> <F7> <Plug>TmuxSendWord
    137     silent! nmap <unique> <silent> <F6> <Plug>TmuxSendWORD
    138     silent! nmap <unique> <silent> <F4> <Plug>TmuxSendSection
    139 endif
    140