17

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.

6
  • 1
    Why would you do that instead of just putting the text directly into the file? Commented May 12, 2011 at 12:42
  • 4
    Do you want user to work with vim, or you want bash script to modify file? In second case, the vim is overkill, you should use ex or sed or perl Commented May 12, 2011 at 12:42
  • I need to write a test script that verifies application like vim editor works well over our file system. Its a testcase for vim editor. Commented May 12, 2011 at 12:44
  • Are you just appending to file? Just do "stuff here" >> file.txt Commented May 12, 2011 at 12:44
  • 4
    re: test if it "works well" : What is your definition of "work well"? If you have vim on your system (check if it exists), and you are running it under a user with the right permissions for whatever file you are editing, it's going to "work well" in that vim will do what it does, no more or less than anywhere else Commented May 12, 2011 at 12:47

5 Answers 5

18

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1, but actually ex is not a version of vim, it is often a symbolic link to vim, and vim will check v:progname to know in what mode it starts.
Thanks. I never knew about ex ! Is it possible to add some content to file with this approach?thanks
that didn't working for me - $ ex file.txt << EEE :i hello :x EEE
12

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

4 Comments

thanks dude.getting little closer :D "vim file.txt -c 'wq' creates and save the file , but how to add a content says "hello world" before closing it? thanks.
vim file.txt -c '$put ="hello world"' -c 'wq'
Is that dependent on vim version ? following still create empty file but no contents.command is - vim file.txt -c '$put="hello"' -c 'wq'
@lakshmipathi: it seems that vim file.txt -c '$put =\'hello\'' -c 'wq' would work better.
9

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}

1 Comment

Years later I regret giving this answer. The truth is you probably shouldn't use vim to do this.
1

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

Comments

1

Recently, I have answered a similar question, “Automated editing of several files in Vim”. May be the solution that I describe there will satisfy your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.