Hostwinds Tutorials

Search results for:


Table of Contents


nano
Create or Edit a File in nano
Saving a File in nano
vi
Create or Edit a File in vi
Saving a File in vi

How to Edit Files from a Linux Shell

Tags: Linux 

nano
Create or Edit a File in nano
Saving a File in nano
vi
Create or Edit a File in vi
Saving a File in vi

Editing text-based files in Linux can be 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 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
nano textfile
  1. You'll see an interface similar to the following screenshot, and you can begin editing the file now.

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

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 an older text editor and the most widely installed. It is a bit less intuitive than nano. However, once you learn a few common commands, you won't find them 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
vi textfilename
  1. You should be greeted with an interface similar to the following.
  1. Before you can begin editing the text, you'll need to press the i  key to switch to INSERT mode, displayed at the bottom of the screen, as shown below.
  1. You can now edit the text in the file.

Saving a File in vi

Once you're done editing a file, you'll need to save the changes 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 —
  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. You will need to enter wq!  and press ENTER on your keyboard to forcefully overwrite the file for read-only files.

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 quit without saving any changes you've made, then use  :q!  instead of  :q

Save and Close:  Hold the shift key 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.

Written by Michael Brower  /  March 28, 2017