Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plugin/autowrite.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function! s:RemoveVimPane()
endfunction

function! s:CurrentVimPane()
return join([s:chomp(system('echo $TMUX_PANE')), s:chomp(system('ps $$ -otty=')), getpid()], " ")
return join([s:chomp(system('echo $TMUX_PANE')), s:chomp(system('ps -otty= $$')), getpid()], " ")
endfunction

function! s:chomp(string)
Expand All @@ -42,4 +42,10 @@ if &term == "screen-256color"
set t_F9=[33~
endif

map <silent> <F19>WriteAll :silent! wall<CR>
nnoremap <silent> <F19>WriteAll :silent! wall<CR>
inoremap <silent> <F19>WriteAll <C-o>:silent! wall<CR>
vnoremap <silent> <F19>WriteAll <Esc>:silent! wall<CR>gv

nnoremap <silent> <F19>AutoReload :silent! checktime<CR>
inoremap <silent> <F19>AutoReload <C-o>:silent! checktime<CR>
vnoremap <silent> <F19>AutoReload <Esc>:silent! checktime<CR>gv
47 changes: 47 additions & 0 deletions tmux-autowrite/autowrite-vim.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
autoload -U add-zsh-hook

# Things that could go wrong:
# √ No vim
# √ Vim is suspended
# √ Multiple vims
# √ A vim doesn't support F-keys
# √ No tmux
# √ Command given to run-shell could return non-zero
# √ No tmux server running
function send_to_vim {
local pane=$1
local tty=$2
local pid=$3

shift 3 # Remaining arguments are keys to send

(ps -ostate=,pid= -t $tty | grep "S \+$pid") >/dev/null 2>&1
local vim_in_foreground=$?

if [[ $vim_in_foreground == 0 ]]; then
tmux send-keys -t $pane $@
fi
}

function trigger_vim_function {
local keys=$1
local pane_list
local panes
pane_list=$(tmux show-environment -g | grep VIM_PANES | cut -f 2 -d =)
panes=("${(s/;/)pane_list}")
for vim_pane in $panes; do
eval send_to_vim $vim_pane $keys
done
true
}

function trigger_autosave {
trigger_vim_function "F19 WriteAll"
}

function trigger_autoreload {
trigger_vim_function "F19 AutoReload"
}

add-zsh-hook preexec trigger_autosave
add-zsh-hook precmd trigger_autoreload