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 -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
But this command open the file and close it ..Which comes in terminal.Is there any solution to avoid that.
Hi,
Set password in your file.
Regards
Sharad
These is possible whit Vim?
Yes! it is possible with both i.e vi and vim
Regards
Sharad
Very useful command. Thanks.
But can you explain the options used?
Hello Akshay,
Welcome.
-c means command to be executed.
Regards
Sharad