vi : Edit file without opening it

In this post I am sharing the trick to edit file without opening it. To achieve this task we will use vi editor. vi is very well known,popular,ultimate and awesome editor and by default available in all Unix/Linux operating system.

Syntax:

vi -c 'COMMAND|wq' /path/of/file

Example: We will replace a keyword with new keyword in file. I hope you know that in vi editor to replace keyword with new keyword in file, we have command syntax as '%s/KEYWORD/NEW_KEYWORD/g'

Here,
File Path = /tmp/test

Given below is the content. We will replace keyword called example with new keyword called trick .


sharad@linuxworld:~$ cat /tmp/test 
This is a test file created for vi example.
In this example we are learning
edit the file with vi without opening it.
sharad@linuxworld:~$

Run the command

vi -c "%s/example/trick/g|wq" /tmp/test

See the below given screenshot for more understanding.

You can observe after running the vi command, example keyword is replace with new keyword called trick .

vi

7 thoughts on “vi : Edit file without opening it”

  1. vi -c “”+y | wq” file path
    here “+y is copy to clipboard command
    here i’m seeing error
    there is no proper ” closing for this. i tried with “\ no use
    can you help me in this
    thank you

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.