Search The Hostwinds Guides Knowledge Base

How to Edit Files from a Linux Shell

Editing text based files in Linux can be a bit confusing if you are unfamiliar with using the command-line as there is no graphical user interface to help guide you. But fear not, we will cover two of the most popular console based text editors, nano and vi, in this article.

  • How to Create or Edit a File
  • How to Save a File

 

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

 

nano

This text editor is a growing favorite among Linux users due to its overall ease of use and is included by default on most modern Linux distributions. nano‘s most commonly used shortcut keys are shown at the bottom of the page with the ^ symbol representing the CTRL key followed by a lowercase letter.

 

Create or Edit a File in nano

  1. On your server’s console, type the nano command followed by the name of the text file, then press ENTER
 

  1. You’ll see an interface similar to the following screenshot and you can begin editing the file now

Showing a blank Nano text editor interface

 

Saving a File in nano

  1. Press the CTRL key and the O key
    1. You’ll be prompted for the name of the file to Save As
    2. When you’re satisfied with the filename press the ENTER key
    3. Then press  the CTRL key and the X key to exit

Showing how to save a file in the Nano text editor

 

Quick commands

Save:  CTRL-O 
This will save the contents to the file you opened, just press ENTER if this is still the file name you wish to edit or CTRL-C to cancel or CTRL-X to exit.

Close: CTRL-X 
This will close the nano program, if you’ve made any unsaved changes it will ask you if you’d like to continue without saving.

 

 

vi

vi is a older text editor and the most widely installed. It is a bit less intuitive than nano, however once you learn a few of the common commands you won’t find it too difficult to use.

 

Create or Edit a File in vi

Editing a file in vi is a fairly straightforward process.

  1. On your server’s console, type the vi command followed by the name of the text 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

 

  1. Before you can begin editing the text you’ll need to press the i  key to switch to INSERT mode which is displayed at the bottom of the screen as shown below

Showing the Insert mode for the Vi text editor

 

  1. You can now edit the text in the file 

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

 

Saving a File in vi

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

  1. You will need to switch out of INSERT mode by pressing the ESC  key before you can close or save the file; it should now no longer show — INSERT —

Showing how to exit Vi's INSERT mode

 

  1. Now issue the write and quit commands
    1. Press the colon ( : ) key on your keyboard (you may need to hold the shift key)
    2. Type wq  and press ENTER on your keyboard so that the command looks like :wq 
    3. Alternatively, you can press SHIFT and hit ZZ to quickly save the file and exit
    4. For read only files, you will need to enter wq!  and press ENTER on your keyboard to forcefully overwrite the file.

 

Showing Vi's write and quit commands

 

Quick commands

Edit: i 
This will place the editor into INSERT mode so you can make changes to the text.

Save:  :wq 
This will save the contents to the file you opened. (write and quit)

Close: :q  or : q! 
This will close the vi program, if you would like to quite without saving any changes you’ve made then use  :q!  instead of  :q 

Save and Close:  Hold the SHIFTkey and press ZZ

 

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

 

Related Articles