znt-tmux.zsh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env zsh
  2. # Copyright (c) 2016, Zsolt Lengyel
  3. # Modifications copyright (c) 2016, Sebastian Gniazdowski
  4. #
  5. # This script opens a new, temporary tmux pane and runs n-history. When
  6. # a selection is made, the result (history entry) is pasted back into
  7. # original tmux pane, and the temporary pane is closed. This allows to
  8. # use local history on remote machines.
  9. #
  10. # To use, put this line to your ~/.tmux.conf. The tool is invoked with:
  11. # Ctrl+b h
  12. #
  13. # bind h run-shell -b "$ZNT_REPO_DIR/doc/znt-tmux.zsh"
  14. #
  15. # get and save the current active tmux pane id
  16. active_pane=$(tmux display -p -F ':#{session_id}:#I:#P:#{pane_active}:#{window_active}:#{session_attached}' )
  17. a_active_pane=("${(@s/:/)active_pane}")
  18. active_session=${a_active_pane[2]//$}
  19. active_window=$a_active_pane[3]
  20. active_pane=$a_active_pane[4]
  21. # set variables for upcoming window
  22. tmux setenv -t $active_session:$active_window.$active_pane "ZNT_TMUX_MODE" 1
  23. tmux setenv -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_SESSION" "$active_session"
  24. tmux setenv -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_WINDOW" "$active_window"
  25. tmux setenv -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_PANE" "$active_pane"
  26. # create a new window in the active session and call it znt-hist
  27. tmux new-window -t $active_session: -n znt-hist
  28. # unset the variables, so only above single window has them
  29. tmux setenv -u -t $active_session:$active_window.$active_pane "ZNT_TMUX_MODE"
  30. tmux setenv -u -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_SESSION"
  31. tmux setenv -u -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_WINDOW"
  32. tmux setenv -u -t $active_session:$active_window.$active_pane "ZNT_TMUX_ORIGIN_PANE"
  33. # znt's session id
  34. znt_active_pane=$(tmux display -p -F ':#{session_id}:#I:#P:#{pane_active}:#{window_active}:#{session_attached}' )
  35. znt_a_active_pane=("${(@s/:/)znt_active_pane}")
  36. znt_active_session=${znt_a_active_pane[2]//$}
  37. znt_active_window=$znt_a_active_pane[3]
  38. znt_active_pane=$znt_a_active_pane[4]
  39. # call znt
  40. tmux send -t "$znt_active_session:$znt_active_window.$znt_active_pane" n-history ENTER