从Bash shell脚本中打开Vim [英] Open Vim from within a Bash shell script

查看:37
本文介绍了从Bash shell脚本中打开Vim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个执行以下操作的Bash shell脚本:

I want to write a Bash shell script that does the following:

  1. 使用Vim打开文件;
  2. 将某些内容写入文件;
  3. 保存文件并退出.

echo 'About to open a file'
vim file.txt  # I need to use vim application to open a file
# Now write something into file.txt
...
# Then close the file.
...
echo 'Done'

有可能吗?我找到了一个叫做Vimscript的东西,但不确定如何使用它.还是可以使用此处文档之类的东西?

Is that possible? I found something called Vimscript, but not sure how to use it. Or something like a here document can be used for this?

更新:我需要验证Vim在我们的文件上是否工作正常系统.所以我需要编写调用Vim的脚本,执行一些命令,并关闭它.我的要求不适合做东西就像 echo'something'>file.txt .我必须使用Vim打开文件.

Update: I need to verify that Vim is working fine over our file system. So I need to write script that invokes Vim, executes some command, and closes it. My requirements do not fit into doing stuffs like echo 'something' > file.txt. I got to open the file using Vim.

推荐答案

Vim有几种选择:

  • -c =>传递ex命令.例如: vim myfile.txt -c'wq'强制文件的最后一行以换行符终止(除非 binary 通过脚本以某种方式设置)
  • -s =>播放用 -W 记录的脚本输出.例如,如果您的文件包含 ZZ ,则 vim myfile.txt -s the_file_ contains_ZZ 将与以前相同.
  • -c => pass ex commands. Example: vim myfile.txt -c 'wq' to force the last line of a file to be newline terminated (unless binary is set in some way by a script)
  • -s => play a scriptout that was recorded with -W. For example, if your file contains ZZ, then vim myfile.txt -s the_file_containing_ZZ will do the same as previously.

还要注意,作为 ex 调用的vim将以ex模式启动;您可以尝试 ex my_file.txt<<<wq

Also note that, invoked as ex, vim will start in ex mode ; you can try ex my_file.txt <<< wq

这篇关于从Bash shell脚本中打开Vim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆