為什麼當我退出 Vim 時 xclip 進程會被殺死?

為什麼當我退出 Vim 時 xclip 進程會被殺死?

如果我在沒有配置的情況下啟動 tmux:

$ tmux -Ltest -f/dev/null new

然後在沒有配置的情況下啟動 Vim:

$ vim -Nu NONE

hello然後從 Vim 執行 xclip 以在剪貼簿中寫入:

:call system('xclip -selection clipboard', 'hello')

然後退出 Vim:

:q

hello仍在剪貼簿中;$ xclip -selection clipboard -o輸出hello


如果我重複相同的實驗,但在 tmux 分割視窗中啟動 Vim(實驗 2):

$ tmux splitw vim -Nu NONE

一旦我退出 Vim,xclip 進程就會終止,剪貼簿為空。


如果我重複相同的實驗,但使用 Nvim(實驗 3),xclip 進程不會被終止,剪貼簿仍然存在。我查看了:h vim-differencesNvim 的文檔並發現了這一點:

|system()|, |systemlist()| can run {cmd} directly (without 'shell')

這讓我認為這個問題是由於 Vim 用 shell 啟動 xclip,而 Nvim 在沒有 shell 的情況下啟動它。為了驗證我的假設,我重複了相同的實驗,但這一次,我在一個檔案中寫入,並使用而不是hello啟動 xclip ,因為前者可以在沒有 shell 的情況下啟動進程(實驗 4):job_start()system()

$ echo hello >/tmp/file
$ tmux -Ltest -f/dev/null new
$ tmux splitw vim -Nu NONE
:call job_start('xclip -selection clipboard /tmp/file')
:q

事實上,退出 Vim 後,xclip 進程仍然存在,並且剪貼簿仍然包含hello.


我想了解為什麼中間 shell 的建立是一個問題。所以,在實驗2,在退出Vim之前,我檢查了xclip進程的祖先:

$ pstree -s -p $(pidof xclip)
systemd(1)---lightdm(951)---lightdm(1126)---upstart(1156)---xclip(15389)

同樣的事情在實驗4:

$ pstree -s -p $(pidof xclip)
systemd(1)---lightdm(951)---lightdm(1126)---upstart(1156)---xclip(18541)

進程樹是相同的,我看不到 Vim 創建的中間 shell(可能是因為它被殺死並且 xclip 被重新設定為新貴進程的父級)。

那麼為什麼當我在實驗 2 中退出 Vim 時 xclip 會被殺死,而當我在實驗 4 中退出 Vim 時 xclip 不會被殺死呢?


編輯:

輸出$ uname -a

Linux ubuntu 4.15.0-51-generic #55~16.04.1-Ubuntu SMP Thu May 16 09:24:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

輸出$ vim --version | head -n2

VIM - Vi IMproved 8.1 (2018 May 18, compiled May 30 2019 04:42:18)
Included patches: 1-1421

輸出$ tmux -V

tmux next-3.1

輸出$ zsh --version

zsh 5.7.1 (x86_64-pc-linux-gnu)

相關內容