我認爲你需要用單引號替換雙引號,以防止你的shell擴展$g
。從man bash
:
Enclosing characters in double quotes preserves the literal value of all
characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !.
目前,你的shell擴展$g
你的字符串裏面,就好像它是一個環境變量。但它可能沒有定義,因此擴展爲空字符串。所以,即使你輸入:
vim -c "2,$g/^dog/d|wq" poo.txt
Vim不接收命令:
2,$g/^dog/d|wq
...但:
2,/^dog/d|wq
此命令刪除所有行地址是2
的那個,到下一個以dog
開頭的(在你的情況下它是第三行)。然後,它保存並退出。
但即使您替換了引號,您的命令仍然存在問題。 從:h :bar
:
These commands see the '|' as their argument, and can therefore not be
followed by another Vim command:
...
:global
...
該杆由:g
解釋爲它的參數的一部分,而不是作爲命令終止。就你而言,這意味着只要找到一行以dog
開頭的行,它就會將其刪除,然後立即保存並退出。因此,如果有多行dog
行,則只有第一行將被刪除,因爲:g
將在處理完第一行後保存並退出。
你需要隱藏|wq
從:g
,無論是包裹在字符串中的全球指揮與:execute
執行它,或者在另一個-c {cmd}
移動wq
。總而言之,你可以嘗試:
vim -c 'exe "2,\$g/^dog/d" | wq' poo.txt
或
vim -c '2,$g/^dog/d' -c 'wq' poo.txt
或
vim -c '2,$g/^dog/d' -cx poo.txt