open vim from within bash shell script

巧了我就是萌 提交于 2019-12-31 09:59:52

问题


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 mybash.sh
      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.


回答1:


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




回答2:


ex is the commandline version for vi, and much easier to use in scripts.

ex $yourfile <<EOEX
  :%s/$string_to_replace/$string_to_replace_it_with/g
  :x
EOEX



回答3:


You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.

To insert text:

ex $yourfile <<EOEX
  :i
  my text to insert
  .
  :x
EOEX

:i enters insert mode. All following lines are inserted text until . is seen appearing by itself on its own line.

Here is how to search and insert as well. You can do something such as:

ex $yourfile <<EOEX
  :/my search query\zs
  :a
  my text to insert
  .
  :x
EOEX

This will find the first selection that matches regex specified by :/, place the cursor at the location specified by \zs, and enter insert mode after the cursor.

You can move \zs to achieve different results. For example:

ex $yourfile <<EOEX
  :/start of match \zs end of match
  :a
  my text to insert 
  .
  :x
EOEX

This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".

If you want to allow any amount of whitespace in your searches between keywords, use \_s*. For example, searching for a function that returns 0: :/\_s*return\_s*0}




回答4:


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 '<ESC>i something to the file<ESC>:wq<CR>'
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:
<ESC> - 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"
<ESC> - exit insert mode
:wq - write the file and quit vim




回答5:


Recently I have answered a similar question, "automated edit of several files". May be solution that I describe there will satisfy your needs.



来源:https://stackoverflow.com/questions/5978108/open-vim-from-within-bash-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!