Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Created May 23, 2020 13:56
Show Gist options
  • Select an option

  • Save Code-Hex/17a66adf4cbcfef1fb0952ed8d0ada46 to your computer and use it in GitHub Desktop.

Select an option

Save Code-Hex/17a66adf4cbcfef1fb0952ed8d0ada46 to your computer and use it in GitHub Desktop.
vim terminal api
" https://github.com/google/vim-maktaba/blob/f630b985facf383ae1657d569ff69582224cca04/autoload/maktaba/path.vim#L9
func s:pathJoin(left, right)
if a:left =~# '\v\\@<!%(\\\\)*\zs/$'
return a:left . a:right
elseif empty(a:left)
return a:right
endif
return a:left . '/' . a:right
endfunc
" for terminal API
func Tapi_open(bufnum, arglist)
if len(a:arglist) < 2
echo 'need at least 2'
endif
let currentdir = a:arglist[0]
for arg in a:arglist[1:]
exe 'tabnew' s:pathJoin(currentdir, arg)
endfor
endfunc
function _send_json_to_vim() {
if (( $# == 0 )) then
echo 'need a json as an argument'
return
fi
local json=$1
if ! jq -e . > /dev/null 2>&1 <<<"$json"; then
echo 'must specify json string'
return
fi
echo -e "\x1b]51;$json\x07"
}
function _vim() {
if [[ "$VIM_TERMINAL" ]] then
local args=$@
local paths=("$PWD" "${args[@]}")
local tapi_args=$(echo "$paths" | perl -pe 's/(.*?)\s+/"$1",/g' | perl -pe 's/,\z//')
_send_json_to_vim "[\"call\",\"Tapi_open\",[$tapi_args]]"
return
fi
vim $@
}
alias vi='_vim'
alias vim='_vim'
@Code-Hex
Copy link
Copy Markdown
Author

こうすると vim8 の :terminal 内で vim コマンドを実行しても、今使ってる vim を使って開いてくれる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment