diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-03-08 02:40:01 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-03-08 02:41:06 -0500 |
commit | d50dd19f6f93becacf30419695e21200dc210a34 (patch) | |
tree | 40282b7d761f4734afd56e9d9f54538b62fe1be5 | |
parent | 1b511a45e7020f7096786d68e939286f9615647b (diff) | |
download | vim-history-sync-d50dd19f6f93becacf30419695e21200dc210a34.tar.gz vim-history-sync-d50dd19f6f93becacf30419695e21200dc210a34.zip |
initial implementation
-rw-r--r-- | LICENSE | 19 | ||||
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | doc/history-sync.txt | 20 | ||||
-rw-r--r-- | plugin/history-sync.vim | 77 | ||||
-rw-r--r-- | sh/history-sync.zsh | 17 |
5 files changed, 148 insertions, 0 deletions
@@ -0,0 +1,19 @@ +Copyright 2018 Jesse Luehrs + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..01445ac --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Overview + +When opening files in Vim from the shell, this plugin adds an entry to Vim's +command history in the form `:e foo.rb` . Also, when opening files in Vim, +adds an entry to the shell history in the form `vim foo.rb` . + +# Installation + +This plugin can be installed as a standard Vim package (see `:help packages`). +Simply clone this repository into ~/.vim/pack/plugins/start/vim-history-sync to +install the Vim files for this plugin. + +To enable this plugin, add `. /path/to/vim-history-sync/sh/history-sync.zsh` +to your .zshrc. Currently only zsh is supported, although patches are welcome +to add support for other shells. diff --git a/doc/history-sync.txt b/doc/history-sync.txt new file mode 100644 index 0000000..7ed0c4f --- /dev/null +++ b/doc/history-sync.txt @@ -0,0 +1,20 @@ +*history-sync.txt* Keeps your shell history and Vim history in sync. + +Author: Jesse Luehrs <https://tozt.net/> +License: MIT + +============================================================================= +OVERVIEW *history-sync-overview* + +When opening files in Vim from the shell, this plugin adds an entry to Vim's +command history in the form `:e foo.rb` . Also, when opening files in Vim, +adds an entry to the shell history in the form `vim foo.rb` . + +============================================================================= +INSTALLATION *history-sync-installation* + +To enable this plugin, add `. /path/to/vim-history-sync/sh/history-sync.zsh` +to your .zshrc. Currently only zsh is supported, although patches are welcome +to add support for other shells. + + vim:tw=78:et:ft=help:norl: diff --git a/plugin/history-sync.vim b/plugin/history-sync.vim new file mode 100644 index 0000000..3f32333 --- /dev/null +++ b/plugin/history-sync.vim @@ -0,0 +1,77 @@ +if $SHELL !~# 'zsh' || !exists('g:_zsh_hist_fname') + finish +endif + +let s:initial_files = {} + +augroup zshhistory + autocmd! + autocmd VimEnter * call <SID>init_zsh_hist() + autocmd BufNewFile,BufRead * call <SID>zsh_hist_append() + autocmd BufDelete * call <SID>remove_initial_file(expand("<afile>")) + autocmd VimLeave * call <SID>reorder_zsh_hist() +augroup END + +function! s:remove_initial_file (file) + if has_key(s:initial_files, a:file) + unlet s:initial_files[a:file] + endif +endfunction + +function! s:get_buffer_list_text () + redir => l:output + ls! + redir END + return l:output +endfunction + +function! s:get_buffer_list () + silent let l:output = <SID>get_buffer_list_text() + let l:buffer_list = [] + for l:buffer_desc in split(l:output, "\n") + let l:name = bufname(str2nr(l:buffer_desc)) + if l:name != "" + call add(l:buffer_list, l:name) + endif + endfor + return l:buffer_list +endfunction + +function! s:init_zsh_hist () + for l:fname in <SID>get_buffer_list() + if strlen(l:fname) > 0 + let s:initial_files[l:fname] = 1 + call histadd(":", "e " . l:fname) + endif + endfor + call delete(g:_zsh_hist_fname) +endfunction + +function! s:zsh_hist_append () + let l:to_append = expand("%:~:.") + " XXX these set buftype too late to be caught by this... + " this is broken, but not sure what a better fix is + if &buftype == '' && l:to_append !~# '^\(__Gundo\|Startify\|\[denite\]\)' + if !has_key(s:initial_files, l:to_append) + if filereadable(g:_zsh_hist_fname) + let l:hist = readfile(g:_zsh_hist_fname) + else + let l:hist = [] + endif + call add(l:hist, l:to_append) + call writefile(l:hist, g:_zsh_hist_fname) + endif + endif +endfunction + +function! s:reorder_zsh_hist () + let l:current_file = expand("%:~:.") + if filereadable(g:_zsh_hist_fname) + let l:hist = readfile(g:_zsh_hist_fname) + if !has_key(s:initial_files, l:current_file) + call filter(l:hist, 'v:val != l:current_file') + endif + call add(l:hist, l:current_file) + call writefile(l:hist, g:_zsh_hist_fname) + endif +endfunction diff --git a/sh/history-sync.zsh b/sh/history-sync.zsh new file mode 100644 index 0000000..586417f --- /dev/null +++ b/sh/history-sync.zsh @@ -0,0 +1,17 @@ +mkdir -p $HOME/.cache/vim/hist +function vim { + local zsh_hist_fname + zsh_hist_fname=$HOME/.cache/vim/hist/$$ + command vim --cmd "let g:_zsh_hist_fname = '$zsh_hist_fname'" "$@" + if [[ -r $zsh_hist_fname ]]; then + while read line; do + if echo $line | grep -q "[[:space:]']"; then + line=${line/\'/\'\\\\\'\'} + line="'$line'" + fi + print -s "vim $line" + done < $zsh_hist_fname + fc -AI + rm -f $zsh_hist_fname + fi +} |