commit 5d2ba6f947f26647b353cbf1498de33b923652d5 Author: benmills Date: Mon Feb 27 20:48:42 2012 -0600 init diff --git a/README.mkd b/README.mkd new file mode 100644 index 0000000..4946a73 --- /dev/null +++ b/README.mkd @@ -0,0 +1,33 @@ +# vimux + +Easily interact with tmux from vim. This project is still in development so some features are still missing. + +## Installation + +With **[vim-bundle](https://github.com/benmills/vim-bundle)**: `vim-bundle install benmills/vimux` + +## Usage + +### RunVimTmuxCommand +Run a system command in a small horizontal split bellow the current pane vim is in. + +*Example:* + +```viml +" Run the current file with rspec +map zr :call RunVimTmuxCommand("clear %% rspec " . bufname("%")) +``` + +### CloseVimTmuxWindows +Close all other tmux panes in the current window. + +*Example:* + +```viml +" Close all other tmux panes in current window +map zc :call CloseVimTmuxWindows() +``` + +## Todo + +* Add the ability to target any tmux session, window and pane diff --git a/plugin/vimux.vim b/plugin/vimux.vim new file mode 100644 index 0000000..cfe5054 --- /dev/null +++ b/plugin/vimux.vim @@ -0,0 +1,98 @@ +if !has("ruby") + finish +end + +function! RunVimTmuxCommand(command) + let g:_VimTmuxCmd = a:command + ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("g:_VimTmuxCmd")) +endfunction + +function! ClearVimTmuxWindow() + unlet g:_VimTmuxRunnerPane +endfunction + +function! CloseVimTmuxWindows() + ruby CurrentTmuxSession.new.close_other_panes + unlet g:_VimTmuxRunnerPane +endfunction + +ruby << EOF +class TmuxSession + def initialize(session, window, pane) + @session = session + @window = window + @pane = pane + if Vim.evaluate('exists("g:_VimTmuxRunnerPane")') != 0 + @runner_pane = Vim.evaluate('g:_VimTmuxRunnerPane') + else + @runner_pane = nil + end + end + + def current_panes + `tmux list-panes`.split("\n").map do |line| + line.split(':').first + end + end + + def active_pane_id + `tmux list-panes`.split("\n").map do |line| + return line.split[-2] if line =~ /\(active\)/ + end + end + + def target(args={}) + "#{args.fetch(:session, @session)}:#{args.fetch(:window, @window)}.#{args.fetch(:pane, @pane)}" + end + + def runner_pane + if @runner_pane.nil? + run("split-window -p 20") + @runner_pane = active_pane_id + Vim.command("let g:_VimTmuxRunnerPane = '#{@runner_pane}'") + end + + `tmux list-panes`.split("\n").map do |line| + return line.split(':').first if line =~ /#{@runner_pane}/ + end + end + + def run_shell_command(command) + send(command, target(:pane => runner_pane)) + move_up_pane + end + + def close_other_panes + run("kill-pane -a") + end + + def move_up_pane + run("select-pane -t #{target}") + end + + def send(command, target) + run("send-keys -t #{target} '#{command.gsub("'", "\'")}'") + run("send-keys -t #{target} Enter") + end + + def run(command) + `tmux #{command}` + end +end + +class CurrentTmuxSession < TmuxSession + def initialize + session = self.get_property(:attached, :session) + window = self.get_property(:active, :window) + pane = self.get_property(:active, :pane) + + super(session, window, pane) + end + + def get_property(match, type) + run("list-#{type.to_s}").split("\n").each do |line| + return line.split(':').first if line =~ /\(#{match.to_s}\)/ + end + end +end +EOF