首页 > 解决方案 > to append the file using vi/vim using shell script

问题描述

I'm trying to write a shell which would append a '.txt' file with some data(stored in a variable). This i'm trying to do using 'vi'. I know there are other tools too to append the file...but i need to use vi only

I tried below command, but unfortunately this command is not inserting the data to the end of the file.:

echo $'i{$var}\E:x\n' |vi file.txt

标签: shellunixvimvi

解决方案


使用vi/vim不允许您就地对文件进行命令行编辑。相反,您可以使用它的命令行等效工具ex( vi-summary.doc ),它应该在任何符合 POSIX 的 shell 中都可用。

cat file
foo
bar

现在ex在命令行中使用该实用程序作为

var=dude
printf '%s\n' '$a' "$var" '.' x | ex file

这将就地编辑文件并在文件dude的最后一行添加文本。

cat file
foo
bar
dude

推荐阅读