LoginSignup
1
2

More than 5 years have passed since last update.

:terminalでファイルを開く

Last updated at Posted at 2018-01-30

Vim の :terminal の中から外の Vim を操る方法
https://mattn.kaoriya.net/software/vim/20171219121032.htm

に記載されている内容ですが、いくつか躓いたのでメモ

普通に:terminalからvimを起動してファイルを開く

それをすると、別のvimがterminalの中で開いてしまって、vim in vim の状態になる。terminalの中で開くのではなく、親vimで開いてほしい。

vim --remote を使う

:terminal cmd /c vim --remote $HOME

確かに開くけど、開いた後にterminalウィンドウは消えてほしい。

++close を使う

:terminalにはオプションとして++closeがつけられる。これをつけるとジョブが終了したらウィンドウも閉じてくれる。

:terminal ++close cmd /c vim --remote $HOME

が、今回の場合はなぜか閉じてくれない。なぜだろう...

vim --remote-send を使う

この記事の最初に貼り付けたURLに記載の通り --remote-send を使ってみる。

:terminal cmd /c vim --remote-send ":e $HOME<CR>"

なんかうまくいかない...

一旦 :terminal でターミナルを起動して vim --remote-send ":e $HOME<CR>" と入力してみる

C:\>vim --remote-send ":e $HOME<CR>"

C:\>:e $HOME
C:\>

あぁ、そうかターミナルに文字列が送信されちゃうのか。ということで頭に<C-W>をつける

C:\>vim --remote-send "<C-W>:e $HOME<CR>"

E948: ジョブはまだ実行中です (! を追加でジョブを終了)

言うとおりにつけてみる。

C:\>vim --remote-send "<C-W>:e! $HOME<CR>"

ちゃんと表示した。でも裏にはバッファとして残ってるみたい。:eをやめて:splitにしてみる。

:terminal ++close vim --remote-send "<C-W>:split $HOME<CR>"

ちゃんと表示して、且つ裏にもバッファは残ってない。

上記を踏まえて peco と組み合わせてみる

カレントディレクトリのファイルのみを表示(dir /a-D /b)してpecoに食わせてフィルタリングしたやつをvimで開く

pcd.bat
@echo off
for /f "usebackq delims==" %%i in (`dir /a-D /b ^| peco`) do (
  vim --servername %VIM_SERVERNAME% --remote-send "<C-W>:split %%i<CR>"
  break
)
:terminal ++close pcd.bat

まとめ

  • :terminalからファイルを親vimで開きたいときは--remote-sendを使う。
  • 送信するコマンドの前には<C-W>を置く
  • :e!だと、裏にバッファが残っちゃうので:splitを使う
  • ++closeしてジョブが終了したときにウィンドウも閉じてもらう
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2