ある文字列をファイルの特定行に挿入するコマンド 〜Vim編〜
ある文字列をファイルの特定行に挿入するコマンド - 元RX-7乗りの適当な日々
元記事はこちら。
元ファイル
$ cat test.txt line1 line2 line3
2行目に挿入
$ vim -u NONE -i NONE -c "2put! ='hoge'" -c wq test.txt $ cat test.txt line1 hoge line2 line3
2行目直下に挿入
$ vim -u NONE -i NONE -c "2put ='hoge'" -c wq test.txt $ cat test.txt line1 line2 hoge line3
"line2"の行前に挿入
$ vim -u NONE -i NONE -c "/^line2$" -c "put! ='hoge'" -c wq test.txt $ cat test.txt line1 hoge line2 line3
"line2"の行後に挿入
$ vim -u NONE -i NONE -c "/^line2$" -c "put ='hoge'" -c wq test.txt $ cat test.txt line1 line2 hoge line3