Open Vim from within a Bash shell script

后端 未结 5 1762
臣服心动
臣服心动 2021-02-01 07:24

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.
5条回答
  •  太阳男子
    2021-02-01 07:59

    If you are wanting to see the work being done inside vim or gvim you can use --remote-send

    gvim --servername SHELL_DRIVER
    bashpromt# cat mybash.sh
    #!/bin/bash
    echo "about to open $1"
    gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
    #now write something into file.txt and close it
    gvim --servername SHELL_DRIVER --remote-send 'i something to the file:wq'
    echo "done."
    

    This will be slow but will do what you want it to.
    First we open a gvim in which we can open all of our files (for efficiency)
    With the first gvim line we open the file in the previously opened gvim.
    On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
    The command is as follows:
    - get out of any mode that gvim might have been in
    i something to the file - go into insert mode and type " something to the file"
    - exit insert mode
    :wq - write the file and quit vim

提交回复
热议问题