How to Edit Files from a Linux Shell

Editing text based files in Linux can be a bit confusing if you’re doing so through shell as there is no graphical user interface to help guide you. There are a few utilities that can be used to edit text based files from the Linux shell, we will cover 2 of these tools in this article; vi and nano. We will cover the following for each text editor:

 

  • How to create a file
  • How to edit a file
  • How to save a file

 

You’ll need to be logged into your server via SSH. If you’re not sure how to do this you may refer to the following guide which covers Connecting to Your Server via SSH.

 

Vi

Vi is a text editor of the screen oriented variety which was initially created for Unix. It is fairly lightweight and easy to use once you understand the basic commands to interact with the tool.

 

Creating a file

Creating a new file in Vi is a fairly straightforward process.

 

  1. Issue the vi command followed by the name of the new file then press “Enter”

 

 

  1. You should be greeted with an interface similar to the following

 

Showing a blank interface for the Vi text editor

 

 

Editing a file

To edit a file please refer to the following.

 

  1. You’ll need to open the file that you wish to edit this can be done in the same manner as creating a file

 

 

  1. Press the i key on your keyboard
  2. You’re now in INSERT mode
    1. Please note that changes made while in INSERT mode are not permanent until the file is saved
    2. You can tell that you’re in INSERT mode by looking towards the bottom of the shell you should see something similar to the following

 

Showing the Insert mode for the Vi text editor

 

  1. You can now enter in text as you’d expect

 

Showing how to insert text while in Vi's insert mode

 

Saving a file

Once you’re done editing a file you’ll need to save the changes that you have made.

 

  1. If you have not exited INSERT mode press the ESC key on your keyboard
    1. You can determine that you’re no longer in INSERT mode by looking toward the bottom of the shell you should no longer see — INSERT —

 

Showing how to exit Vi's INSERT mode

 

  1. You’ll then need to issue the write and quit commands
    1. Press : on your keyboard (you may need to hold the shift key)
    2. Type wq! and press Enter on your keyboard

 

Showing Vi's write and quit commands

 

GNU nano

Often referred to as just nano, this text editor is a growing favorite among Linux users due to its overall ease of use. Like vi, nano was created for Unix based systems and is used through the command line interface.

 

Creating a file

You can create a new file in nano quite easily with one simple command.

 

  1. Issue the nano command followed by the name of the new file then press Enter

 

 

  1. You’ll see an interface similar to the following screenshot

 

Showing a blank Nano text editor interface

 

Editing a file

Unlike vi, editing a file in nano is very simple. You can literally just start typing with no need for any special key combinations. Neat!

 

Saving a file

Saving a file does require specific key combinations, however these are not complicated. Once you’re ready to save your file, refer to the following steps.

 

  1. Press the CTRL key and the O key on your keyboard
    1. You’ll be prompted for a filename to write

 

Showing how to save a file in the Nano text editor

 

  1. When you’re satisfied with the filename press the Enter key on your keyboard
  2. Then press  the CTRL key and the X key on your keyboard to exit

 

You should now be able to successfully create, edit, and save a file in two of the primarily used text based editors in Linux.


Share:

Leave a Reply

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