├── .github └── workflows │ └── assign.yml ├── .gitignore ├── LICENSE ├── README.md ├── autoload └── editerm.vim ├── doc ├── vim-editerm.jax └── vim-editerm.txt └── plugin ├── editerm.vim ├── setup.sh ├── tdrop └── tvim /.github/workflows/assign.yml: -------------------------------------------------------------------------------- 1 | name: Issue assignment 2 | on: 3 | issues: 4 | types: [opened] 5 | jobs: 6 | auto-assign: 7 | runs-on: ubuntu-latest 8 | permissions: 9 | issues: write 10 | steps: 11 | - name: 'Auto-assign issue' 12 | uses: pozil/auto-assign-issue@v1 13 | with: 14 | assignees: kyoh86 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /doc/tags* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 kyoh86 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-editerm 2 | 3 | A plugin sets `$EDITOR` in `:terminal` to open a file in the working vim instead of a new one. 4 | 5 | Usually, calling `git commit`, `vim` and suchlike commands in |:terminal|, 6 | new vim process starts in their. 7 | But it makes some unfavorable situation depending on the settings. 8 | 9 | This plugin, *vim-editerm* will set an environment varialble `$EDITOR` and 10 | override `vim` command with function that drops files in new buffer in parent 11 | vim. 12 | 13 | ## Screenshot 14 | 15 | ![](https://user-images.githubusercontent.com/5582459/63220140-76a44400-c1bc-11e9-8555-389aafde8a38.gif) 16 | 17 | ## INSTALLATION 18 | 19 | With vim-plug: 20 | 21 | ``` 22 | Plug 'kyoh86/vim-editerm' 23 | ``` 24 | 25 | And if you want to override `vim` and create commands `:edit|new|vnew|tabnew` in `:terminal`, put below lines in `.zshrc` or `.bashrc`. 26 | 27 | ``` 28 | if [[ "${VIM_EDITERM_SETUP}" != "" ]]; then 29 | source "${VIM_EDITERM_SETUP}" 30 | fi 31 | ``` 32 | 33 | ## USAGE 34 | 35 | Without being conscious. 36 | 37 | In `:terminal`, you can `git commit` in buffer in the parent vim. 38 | 39 | If you want to stop editing with the error (i.e. to stop to commit), call `:Cq` or `:CQ`. 40 | 41 | ## CONFIGURATION 42 | 43 | ### Behavior to open files 44 | 45 | We can change behavior to open files. 46 | 47 | ```vim 48 | let g:editerm_opener = 'n' " :new (default) 49 | let g:editerm_opener = 'e' " :edit 50 | let g:editerm_opener = 'v' " :vnew 51 | let g:editerm_opener = 't' " :tabnew 52 | ``` 53 | 54 | Or by `.zshrc` or `.bashrc` 55 | 56 | ```sh 57 | export VIM_EDITERM_OPENER="n" # :new 58 | export VIM_EDITERM_OPENER="e" # :edit 59 | export VIM_EDITERM_OPENER="v" # :vnew 60 | export VIM_EDITERM_OPENER="t" # :tabnew 61 | ``` 62 | 63 | ### Definition of the dropping commands 64 | 65 | `vim-editerm` will create below commands which drop files to vim. 66 | 67 | ```shell 68 | :drop 69 | :edit 70 | :new 71 | :vnew 72 | :tabnew 73 | ``` 74 | 75 | If you want to supress them: 76 | 77 | ```vim 78 | let g:editerm_defdrop = '0' " default: '1' 79 | ``` 80 | 81 | And if you want to change prefix of them: 82 | 83 | ```vim 84 | let g:editerm_defdrop_prefix = 'vim_' " default: ':' 85 | ``` 86 | 87 | ## Similar projects 88 | 89 | - [lambdalisue/edita.vim](https://github.com/lambdalisue/edita.vim) 90 | - **no external dependencies** 91 | 92 | # LICENSE 93 | 94 | [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg)](http://www.opensource.org/licenses/MIT) 95 | 96 | This software is released under the [MIT License](http://www.opensource.org/licenses/MIT), see LICENSE. 97 | -------------------------------------------------------------------------------- /autoload/editerm.vim: -------------------------------------------------------------------------------- 1 | " editerm.vim - 2 | " Maintainer: kyoh86 3 | " License: MIT License(http://www.opensource.org/licenses/MIT) 4 | 5 | scriptversion 3 6 | 7 | if exists('g:autoloaded_editerm') 8 | finish 9 | endif 10 | let g:autoloaded_editerm = 1 11 | 12 | let s:save_cpo = &cpo 13 | set cpo&vim 14 | 15 | let s:remotes={} 16 | 17 | function! editerm#kill_remote(bang) 18 | let l:bufnr = bufnr('%') 19 | call editerm#release_remote(l:bufnr, '1') 20 | execute(l:bufnr .. get(g:, 'editerm_killer', 'bwipeout') .. a:bang) 21 | endfunction 22 | 23 | function! editerm#leave_remote() 24 | call editerm#release_remote(expand('')+0, '0') 25 | endfunction 26 | 27 | function! editerm#release_remote(bufnum, ret) 28 | if !has_key(s:remotes, a:bufnum) 29 | return 30 | endif 31 | 32 | " lockfile for the buffer 33 | let l:locker = s:remotes[a:bufnum] 34 | if l:locker != '' 35 | " update lockfile 36 | call writefile([a:ret], l:locker) 37 | endif 38 | unlet s:remotes[a:bufnum] 39 | endfunction 40 | 41 | function! editerm#open(command, lockfile, filename, options) 42 | let l:command = get({ 43 | \ 'n': 'new', 44 | \ 'e': 'edit', 45 | \ 'v': 'vnew', 46 | \ 't': 'tabnew' 47 | \ }, a:command, '') 48 | if l:command == '' 49 | echoerr 'invalid command: ' .. a:command 50 | return 51 | endif 52 | 53 | execute(l:command .. ' ' .. a:options .. ' ' .. a:filename) 54 | 55 | if a:lockfile != '' 56 | let s:remotes[bufnr('%')] = a:lockfile 57 | setlocal bufhidden=wipe 58 | command -buffer -bang Cq :call editerm#kill_remote('') 59 | command -buffer -bang CQ :call editerm#kill_remote('') 60 | augroup EDITERM 61 | autocmd! * 62 | autocmd BufUnload :call editerm#leave_remote() 63 | augroup END 64 | endif 65 | endfunction 66 | 67 | let &cpo = s:save_cpo 68 | unlet s:save_cpo 69 | -------------------------------------------------------------------------------- /doc/vim-editerm.jax: -------------------------------------------------------------------------------- 1 | *vim-editerm.txt* :terminal の中で $EDITOR を外のVimに置き換えるプラグイン 2 | 3 | ============================================================================== 4 | 目次 *vim-editerm-contents* 5 | 6 | - はじめに |vim-editerm-introduction| 7 | - インストール |vim-editerm-install| 8 | - コマンド |vim-editerm-commands| 9 | - Cq |:Cq| |:CQ| 10 | - シェル |vim-editerm-shell-functions| 11 | - 設定 |vim-editerm-options| 12 | - g:editerm_opener |g:editerm_opener| 13 | - g:editerm_defdrop |g:editerm_defdrop| 14 | - g:editerm_defdrop_prefix |g:editerm_defdrop_prefix| 15 | - LICENSE |vim-editerm-license| 16 | 17 | ============================================================================== 18 | はじめに *vim-editerm-introduction* 19 | 20 | 通常、 |:terminal| の中で `git commit` などのコマンドを使用すると、ターミナル 21 | の中でさらにVimが起動してしまいます。 22 | その場合、設定などによっては思わぬ挙動を示すことがあります。 23 | 24 | このプラグイン *vim-editerm* は、 |:terminal| の中の環境変数 `$EDITOR` を自動 25 | で変更し、 `git commit` などのコマンドでVimを新たに起動するのではなく、外のVim 26 | にファイル編集を受け渡すようにすることができるプラグインです。 27 | 28 | ============================================================================== 29 | インストール *vim-editerm-install* 30 | 31 | `vim-plug` を使用する場合は以下の通りです。 > 32 | Plug 'kyoh86/vim-editerm' 33 | < 34 | *vim-editerm-shell-init* 35 | `:terminal` の中で `vim` コマンドを上書きしたり、`:edit|new|vnew|tabnew` 36 | といったコマンドを使えるようにするには、`.zshrc` や `.bashrc` 37 | に以下を記述してください。 > 38 | if [[ "${VIM_EDITERM_SETUP}" != "" ]]; then 39 | source "${VIM_EDITERM_SETUP}" 40 | fi 41 | < 42 | ============================================================================== 43 | コマンド *vim-editerm-commands* 44 | 45 | Cq *:Cq* *:CQ* 46 | 47 | `$EDITOR` で開始したファイルの編集を、キャンセルします。 48 | ファイルに変更が加わっている場合は、 `:CQ!` と || をつけて 49 | 強制的に終了することもできます。 50 | 51 | このコマンドを実行すると、 `$EDITOR` を使用しているコマンドに 52 | エラーコードを返すため、 `git commit` などの実行がキャンセルされます。 53 | 54 | ============================================================================== 55 | シェル *vim-editerm-shell-functions* 56 | 57 | `.zshrc` や `.bashrc` に初期化処理( |vim-editerm-shell-init| )を記述した 58 | 場合、次のコマンドを使用できるようになります。 59 | 60 | > 61 | :drop 62 | :edit 63 | :new 64 | :vnew 65 | :tabnew 66 | < 67 | `:drop` は |g:editerm_opener| で設定したデフォルトの挙動でファイルを開きます。 68 | それ以外のコマンドはVimの同名のコマンドと同等です。 69 | 70 | ============================================================================== 71 | オプション *vim-editerm-options* 72 | 73 | g:editerm_opener *g:editerm_opener* 74 | 型: |String| 75 | デフォルト: `'n'` 76 | 77 | ファイルを開くときの挙動を変更します。 78 | 79 | 選択肢: 80 | `'e'`: 現在のバッファ(:terminal)で開きます。 `:edit` を使用します。 81 | `'n'`: 水平に分割したウィンドウで開きます。 `:new` を使用します。 82 | `'v'`: 垂直に分割したウィンドウで開きます。 `:vnew` を使用します。 83 | `'t'`: 新しいタブで開きます。 `:tabnew` を使用します。 84 | 85 | 例: > 86 | let g:editerm_opener = 't' " 新しいタブで開く 87 | < 88 | これらのオプションは、 |:terminal| 内の環境変数 `$VIM_EDITERM_OPENER` でも 89 | 設定を変更することができます。 90 | 91 | 例: > 92 | export VIM_EDITERM_OPENER="v" # 垂直に分割したウィンドウで開く 93 | < 94 | g:editerm_defdrop *g:editerm_defdrop* 95 | 型: |String| 96 | デフォルト: `'1'` 97 | 98 | `vim-editerm` はシェルに、外のVimにファイルを受け渡すための特殊なコマンド 99 | ( |vim-editerm-shell-functions| )を定義します。 100 | 101 | これらを削除したい場合、 `g:editerm_defdrop` を `0` に設定します。 102 | 103 | 例: > 104 | let g:editerm_defdrop = '0' 105 | 106 | g:editerm_defdrop_prefix *g:editerm_defdrop_prefix* 107 | 型: |String| 108 | デフォルト: `':'` 109 | 110 | `vim-editerm` はシェルに、外のVimにファイルを受け渡すための特殊なコマンド 111 | (|vim-editerm-shell-functions|)を定義します。 112 | 113 | これらのコマンドの接頭辞(`:`)を変更したい場合、 114 | `g:editerm_defdrop_prefix` を変更します。 115 | 116 | 例: > 117 | let g:editerm_defdrop_prefix = 'vim_' 118 | # ":drop" ":edit" などのコマンドを "vim_drop" "vim_edit" に置き換える 119 | < 120 | ============================================================================== 121 | LICENSE *vim-editerm-license* 122 | 123 | MIT License. 124 | 125 | Also see http://www.opensource.org/licenses/MIT for more information. 126 | 127 | vim:textwidth=78:tabstop=4:shiftwidth=4:expandtab:filetype=help:norl: 128 | -------------------------------------------------------------------------------- /doc/vim-editerm.txt: -------------------------------------------------------------------------------- 1 | *vim-editerm.txt* Plugin sets $EDITOR using parent vim in :terminal 2 | 3 | ============================================================================== 4 | CONTENTS *vim-editerm-contents* 5 | 6 | - Introduction |vim-editerm-introduction| 7 | - Install |vim-editerm-install| 8 | - Commands |vim-editerm-commands| 9 | - Cq |:Cq| |:CQ| 10 | - Shell Functions |vim-editerm-shell-functions| 11 | - Options |vim-editerm-options| 12 | - g:editerm_opener |g:editerm_opener| 13 | - g:editerm_defdrop |g:editerm_defdrop| 14 | - g:editerm_defdrop_prefix |g:editerm_defdrop_prefix| 15 | - License |vim-editerm-license| 16 | 17 | ============================================================================== 18 | INTRODUCTION *vim-editerm-introduction* 19 | 20 | Usually, calling `git commit`, `vim` and suchlike commands in |:terminal|, new 21 | vim process starts in their. 22 | But it makes some unfavorable situation depending on the settings. 23 | 24 | This plugin, *vim-editerm* will set an environment varialble `$EDITOR` and 25 | override `vim` command with function that drops files in new buffer in parent 26 | vim. 27 | 28 | ============================================================================== 29 | INSTALL *vim-editerm-install* 30 | 31 | Below is a sample using vim-plug > 32 | Plug 'kyoh86/vim-editerm' 33 | < 34 | *vim-editerm-shell-init* 35 | And if you want to override `vim` and create commands `:edit|new|vnew|tabnew` 36 | in `:terminal`, put below lines in `.zshrc` or `.bashrc`. > 37 | if [[ "${VIM_EDITERM_SETUP}" != "" ]]; then 38 | source "${VIM_EDITERM_SETUP}" 39 | fi 40 | < 41 | ============================================================================== 42 | COMMANDS *vim-editerm-commands* 43 | 44 | Cq *:Cq* *:CQ* 45 | 46 | This commands will be respond error to commands calling `$EDITOR`. 47 | 48 | e.g. To stop committing by `git commit`. 49 | 50 | ============================================================================== 51 | SHELL FUNCTIONS *vim-editerm-shell-functions* 52 | 53 | `vim-editerm` will create below commands which drop files to vim. 54 | > 55 | :drop 56 | :edit 57 | :new 58 | :vnew 59 | :tabnew 60 | < 61 | ============================================================================== 62 | OPTIONS *vim-editerm-options* 63 | 64 | g:editerm_opener *g:editerm_opener* 65 | Type: |String| 66 | Default: `'n'` 67 | 68 | Change behavior to open new buffer. 69 | 70 | Options: 71 | `'e'`: open new buffer in current window. 72 | `'n'`: split window horizontally. 73 | `'v'`: split window vertically. 74 | `'t'`: open new tab. 75 | 76 | Example: > 77 | let g:editerm_opener = 't' " open new tab 78 | < 79 | This option can be overwritten by the shell variable `$VIM_EDITERM_OPENER`. 80 | 81 | Example: > 82 | export VIM_EDITERM_OPENER="v" # split window vertically 83 | < 84 | g:editerm_defdrop *g:editerm_defdrop* 85 | Type: |String| 86 | Default: `'1'` 87 | 88 | `vim-editerm` will create special commands which drop files to vim. 89 | (See:|vim-editerm-shell-functions|) 90 | 91 | To suppress them, you can set `g:editerm_defdrop` to `0`. 92 | 93 | Example: > 94 | let g:editerm_defdrop = '0' 95 | 96 | g:editerm_defdrop_prefix *g:editerm_defdrop_prefix* 97 | Type: |String| 98 | Default: `':'` 99 | 100 | `vim-editerm` will create special commands which drop files to vim. 101 | (See:|vim-editerm-shell-functions|) 102 | 103 | To change their prefix (`:`), you can set `g:editerm_defdrop_prefix`. 104 | 105 | Example: > 106 | let g:editerm_defdrop_prefix = 'vim_' 107 | # This will create commands like "vim_drop", "vim_edit" and so on. 108 | < 109 | ============================================================================== 110 | LICENSE *vim-editerm-license* 111 | 112 | MIT License. 113 | 114 | Also see http://www.opensource.org/licenses/MIT for more information. 115 | 116 | vim:textwidth=78:tabstop=4:shiftwidth=4:expandtab:filetype=help:norl: 117 | -------------------------------------------------------------------------------- /plugin/editerm.vim: -------------------------------------------------------------------------------- 1 | " editerm.vim - 2 | " Maintainer: kyoh86 3 | " License: MIT License(http://www.opensource.org/licenses/MIT) 4 | 5 | if exists('g:loaded_editerm') 6 | finish 7 | endif 8 | let g:loaded_editerm = 1 9 | 10 | let s:save_cpo = &cpo 11 | set cpo&vim 12 | 13 | scriptversion 3 14 | 15 | " Function: Tapi_EditermEditFile 16 | " Description: It should be called from :terminal with three parameters 17 | " [(n|e|v|t), (lock file), (editing file)] as $EDITOR. 18 | function! Tapi_EditermEditFile(bufnum, arglist) 19 | if len(a:arglist) == 4 20 | call editerm#open(a:arglist[0], a:arglist[1], a:arglist[2], a:arglist[3]) 21 | endif 22 | endfunction 23 | 24 | let s:dir = expand(':p:h') 25 | let $EDITOR = s:dir .. '/tvim' 26 | let $VIM_EDITERM_SETUP = s:dir .. '/setup.sh' 27 | let $VIM_EDITERM_DROP = s:dir .. '/tdrop' 28 | 29 | let $VIM_EDITERM_OPENER = get(g:, 'editerm_opener', 'n') 30 | let $VIM_EDITERM_DEFDROP = get(g:, 'editerm_defdrop', '1') 31 | let $VIM_EDITERM_DEFDROP_PREFIX = get(g:, 'editerm_defdrop_prefix', ':') 32 | 33 | let &cpo = s:save_cpo 34 | unlet s:save_cpo 35 | -------------------------------------------------------------------------------- /plugin/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # setup.sh 4 | # Maintainer: kyoh86 5 | # License: MIT License(http://www.opensource.org/licenses/MIT) 6 | 7 | if [ -n "${VIM_TERMINAL}" ]; then 8 | vim() { 9 | has_option=0 10 | for x; do 11 | case $x in 12 | -- ) 13 | : 14 | ;; 15 | -* ) 16 | has_option=1 17 | ;; 18 | esac 19 | done 20 | if [ $has_option = 1 ] ; then 21 | command vim "$@" 22 | else 23 | ${EDITOR} "$@" 24 | fi 25 | } 26 | if [ "${VIM_EDITERM_DEFDROP}" = "1" ]; then 27 | eval "function ${VIM_EDITERM_DEFDROP_PREFIX}drop()"' { ${VIM_EDITERM_DROP} "$@"; }' 28 | eval "function ${VIM_EDITERM_DEFDROP_PREFIX}edit()"' { VIM_EDITERM_OPENER=e ${VIM_EDITERM_DROP} "$@"; }' 29 | eval "function ${VIM_EDITERM_DEFDROP_PREFIX}new()"' { VIM_EDITERM_OPENER=n ${VIM_EDITERM_DROP} "$@"; }' 30 | eval "function ${VIM_EDITERM_DEFDROP_PREFIX}vnew()"' { VIM_EDITERM_OPENER=v ${VIM_EDITERM_DROP} "$@"; }' 31 | eval "function ${VIM_EDITERM_DEFDROP_PREFIX}tabnew()"' { VIM_EDITERM_OPENER=t ${VIM_EDITERM_DROP} "$@"; }' 32 | fi 33 | fi 34 | -------------------------------------------------------------------------------- /plugin/tdrop: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # tdrop 4 | # Maintainer: kyoh86 5 | # License: MIT License(http://www.opensource.org/licenses/MIT) 6 | 7 | if [ $# -eq 0 ]; then 8 | if [ "$VIM_EDITERM_OPENER" = 'n' ] || \ 9 | [ "$VIM_EDITERM_OPENER" = 'v' ] || \ 10 | [ "$VIM_EDITERM_OPENER" = 't' ] ; then 11 | echo -e "\033]51;[\"call\", \"Tapi_EditermEditFile\", [\"${VIM_EDITERM_OPENER}\", \"\", \"\", \"\"]]\007" 12 | fi 13 | fi 14 | 15 | for file; do 16 | if [ "${file:0:1}" != "/" ]; then 17 | file="${PWD%/}/${file}" 18 | fi 19 | 20 | echo -e "\033]51;[\"call\", \"Tapi_EditermEditFile\", [\"${VIM_EDITERM_OPENER}\", \"\", \"$file\", \"\"]]\007" 21 | done 22 | -------------------------------------------------------------------------------- /plugin/tvim: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # tvim 4 | # Maintainer: kyoh86 5 | # License: MIT License(http://www.opensource.org/licenses/MIT) 6 | 7 | exit_code=0 8 | for file; do 9 | tmpfile=$(mktemp) 10 | 11 | if [ "${file}" = "--" ]; then 12 | continue 13 | fi 14 | 15 | if [[ "${file}" =~ ^\+[0-9]+$ ]]; then 16 | opts="${opts:-} ${file}" 17 | continue 18 | fi 19 | 20 | if [ -z "${file}" ]; then 21 | continue 22 | fi 23 | 24 | if [ "${file:0:1}" != "/" ]; then 25 | file="${PWD%/}/${file}" 26 | fi 27 | 28 | echo -e "\033]51;[\"call\", \"Tapi_EditermEditFile\", [\"${VIM_EDITERM_OPENER}\", \"${tmpfile}\", \"$file\", \"$opts\"]]\007" 29 | 30 | if command -v inotifywait >/dev/null; then 31 | inotifywait -e modify "${tmpfile}" 32 | elif command -v fswatch >/dev/null; then 33 | fswatch -1 -0 --event Updated --format '' "${tmpfile}" 34 | else 35 | while true; do 36 | if [ ! -f "${tmpfile}" ]; then 37 | break 38 | fi 39 | if [ -s "${tmpfile}" ]; then 40 | break 41 | fi 42 | sleep 1 43 | done 44 | fi 45 | 46 | if [ -s "${tmpfile}" ]; then 47 | while read -r line; do 48 | if [ "${line}" = "" ]; then 49 | continue 50 | fi 51 | exit_code=$((line)) 52 | break 53 | done < "${tmpfile}" 54 | fi 55 | rm "${tmpfile}" 56 | 57 | if [ ${exit_code} != 0 ]; then 58 | break 59 | fi 60 | done 61 | 62 | exit $exit_code 63 | --------------------------------------------------------------------------------