How to Edit a File on Command line

September 21, 2023 / How-to Guide

In this guide, you will understand how to edit a file on command line using the Vi and Nano commands.

Follow the steps-

1) Using Vi command-

  1. Access the file using the Vi or Vim command-
    vi example_file
  2. To enter insert mode, press the “I” key.
  3. Apply your preferred modifications. You can utilise the arrow keys to navigate within the file.
  4. To exit insert mode, press “ESC” key.
  5. Enter “:w” and press the “Enter key” to save the changes.
  6. Enter “:q” and press the “Enter” key to close this file.
  7. You can enter “:wq” to save and close the file.
  8. Keep in mind that if you make unintended changes, you can abort them by using “:q!” instead of “:w” or “:wq.” This command will exit Vi without saving the file.

2) Using Nano command-

  1. Access the file using the Nano command-
    nano example_file
  2. Apply your preferred changes. You can utilise the arrow keys to navigate within the file.
  3. To close the file, press the “CTRL + X” keys. You will be prompted to save the changes. To save the changes, press “y” key and then to exit press enter key.

Was this article helpful to you? If you liked this article, then you will surely love our support. Get in touch of eukhost’s support experts; they are available 24×7.

Spread the love