6

Hi I have a situation as

I have a writing a shell script where I have to pass input to the vim.

Explaining in detail

Here I have already written shell script that I cannot changed. Here is the code for it.

 sudo vim /mnt/etc/{hosts,hostname,ports}

due to this hosts file is opened we can go manually next to the hostname file by :n and similar for the ports file.

But I have to perform same operation from my .sh file. Also I have to edit the ports file and after completing it I have to save and quite it through :wq command.

How can I do it?

7
  • Do you mean open the file interactively, or make edits directly in the script file? If the latter, can you give us more details on the edits you need to make? If the former, why can't you just put that command as-is in your .sh file and run it? Commented Dec 16, 2013 at 15:33
  • @Donovan From the title I thought it was the latter — “execute… commands through shell script”. Commented Dec 16, 2013 at 15:36
  • 1
    it's not clear, and vi is not intended to be used that way. It's a visual editor. If you need to modify file contents programmatically, sed (the stream editor) would be the way to do it. Commented Dec 16, 2013 at 15:41
  • @Donovan it means it is not possible to do it through vim? or i have to changed the above command. Commented Dec 16, 2013 at 15:47
  • If you are writing a shell script it is because you want to automatize a task, but editing with vim is not something that can/should be automatized. If the core of the task is text processing, add or edit, you can use other tools like sed, awk, cat >>. Commented Dec 16, 2013 at 15:49

1 Answer 1

6

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

That said, you can use Vim non-interactively:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

Full Automation

For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE        Do not load vimrc and plugins, alternatively:
--noplugin        Do not load plugins.
-n                No swapfile.
-es               Ex mode + silent batch mode -s-ex
                Attention: Must be given in that order!
-S ...            Source script.
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                with messages or output to avoid blocking.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply I have used sed and made some changes finally it worked.

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.