• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

vi : Edit file without opening it

July 6, 2014 by Sharad Chhetri 7 Comments

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

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Edit the crontab without using crontab -e
  2. Yum command to download rpm file without installing in linux system
  3. System settings not opening on Ubuntu 16.04 LTS
  4. Could not find make anywhere!!! Please edit the config section to include the path to make. at ./install.pl line 2101
  5. NOTICE: nagios.cmd file not found. Please specify the location of this file in your /etc/vshell.conf file
  6. Read the file inside compressed .gz files without extract
  7. Add swap file in linux without reboot after OS installation
  8. How to enable direct ssh to EC2 linux instance without keypair file
  9. Give permission reference to file/dir from other file/dir in linux
  10. group ownership reference to file/dir from other file/dir in linux

Filed Under: Linux, Linux Commands Tagged With: linux command, vi

Reader Interactions

Comments

  1. ajay says

    December 4, 2019 at 11:44 am

    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
  2. Soumi says

    April 3, 2018 at 7:07 am

    But this command open the file and close it ..Which comes in terminal.Is there any solution to avoid that.

    Reply
    • Sharad Chhetri says

      April 7, 2018 at 5:01 pm

      Hi,

      Set password in your file.

      Regards
      Sharad

      Reply
  3. Rafael Barrientos says

    January 25, 2018 at 4:30 pm

    These is possible whit Vim?

    Reply
    • Sharad Chhetri says

      January 25, 2018 at 5:17 pm

      Yes! it is possible with both i.e vi and vim

      Regards
      Sharad

      Reply
  4. akshay says

    October 29, 2015 at 8:40 am

    Very useful command. Thanks.
    But can you explain the options used?

    Reply
    • sharad chhetri says

      October 31, 2015 at 6:11 am

      Hello Akshay,

      Welcome.
      -c means command to be executed.

      Regards
      Sharad

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to change hostname in Ubuntu 12.04 and 12.10 without system restart

Keep logs of user after sudo su – : Secondary Logging

rsync all files,hidden files,symlinks,hardlinks to remotes Linux Server

How to set JAVA environment variables in Linux

master admin password openerp 7.x

send email after mysql backup through bash script in simple way

Virtual Machine inaccessible status on VirtualBox 4.3

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy