linuxbashvim

Open Vim from within a Bash shell script


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

  1. Opens a file using Vim;
  2. Writes something into the file;
  3. Saves the file and exits.
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'

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?

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 need to open the file using Vim.


Solution

  • Vim has several options:

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