lakshmipathi lakshmipathi - 1 year ago 56
Linux Question

open vim from within bash shell script

I want bash shell that does the following

  1. open a file using vim

  2. write something into file

  3. save and exit the file

    bashpromt# cat
    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
    #close the that file.
    echo "done."

IS that possible? I found something like vim script , but not sure how to use it.
Or something like "HERE" doc can be used for this ?

EDIT: I need to verify that vim editor is working fine over our file system.So I need to write script that invokes vim and does some command and close it. My requirement doesn't fit into doing stuffs like "echo "something" > file.txt" . I got to open the file using vim.

Answer Source

Vim has several options:

  • -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.

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